天天看點

程式員必看之★程式設計原則(中英文對照)★--《Enough Rope to Shoot Yourself in the Foot

名字和辨別

44 Names should be common English words, descriptive of what the function, argument, or

variable does

44 名字應該是普通英文單詞,用來描述這個函數、參數或者變量的作用

44.1.Do not clutter names with gibberish

44.1 不要亂起名字(無意義的,深奧的,不貼切的)

45 Macro names should be ENTIRELY_CAPITALIZED

45 宏名字應該形如:ENTIRELY_CAPITALIZED

45.1 Do not capitalize members of an enum

45.1 不要将枚舉類型成員大寫

45.2 Do not capitalize type names created with a typedef

45.2 不要将用typedef定義的類型大寫

46 Avoid the ANSI C name space

46 避免ANSI C名字空間

47 Avoid the Microsoft name space

47 避免微軟名字空間

48 Avoid unnecessary symbols

48避免不必要的符号

49 Symbolic constants for Boolean values are rarely necessary

49 布爾型符号常量基本上沒有用

一般程式設計原則

50 Don't confuse familiarity with readability

50 熟悉代碼與代碼的可讀性好是兩回事

51 A function should do only one thing

51 一個函數隻應該完成一件事

52 Too many levels of abstraction or encapsulation are as bad as too few

52 過多或者過少的抽象或者封裝層次都不好

53 A function should be called more than once, but?br>

53 一個函數應該被多處調用

53.1 Code used more than once should be put into a function

53.1 在多于一處使用的代碼應該變成一個函數

54 A function should have only one exit point

54 一個函數應該隻有一個退出點

54.1 Always put a return at the outer level

54.1 總是在外層放一個return

55 Avoid duplication of effort

55 避免費兩遍事的事情

56 Don't corrupt the global name space

56 不要污染全局名字空間

56.1 Avoid global symbols

56.1 避免全局符号

56.2 Never require initialization of a global variable to call a function

56.2 對函數的調用不應該依賴全局變量是否被初始化

56.2.1 Make locals static in recursive functions if the value doesn't span a recursive call

56.2.1 在第歸函數中,如果一個變量與第歸調用無關,則其應該是局部靜态變量

56.3 Use instance counts in place of initialization functions

56.3 在初始化函數中使用執行個體計數

56.4 If an if ends in return, don't use else

56.4 如果if語句中有return,則不要再寫else

57 Put the shortest clause of an if/else on top

57 最短的if/else子句在最上面

58 Try to move errors from run time to compile time

58 盡量将運作時刻的錯誤提前到編譯時刻

59 Use C function pointers as selectors

59 使用C 函數指針作為選擇器

60 Avoid do/while loops

60 避免do/while循環

60.1 Never use a do/while for a forever loop

60.1 不要用do/while實作無限循環

61 Counting loops should count down if possible

61 循環計數應該從大到小

62 Don't do the same thing in two ways at the same time

62 在同一時間不要用不同的方法完成同一件事

63 Use for if any two of an initialization, test, or increment are present

63 如果初始化、測試、增量三者有兩個,就應該用for語句

64 If it doesn't appear in the test, it shouldn't appear in the other parts of for statement

64 沒有在for語句的測試部分出現的變量,也不應該出現在其他兩個部分

65 Assume that things will go wrong

65 總是假定事情要出錯

66 Computers do not know mathematics

66 計算機不懂得數學

66.1 Expect the impossible

66.1 總是會出現不可能的事情

66.2 Always check error-return codes

66.2 檢查錯誤傳回代碼

67 Avoid explicit temporary variables

67 避免顯式臨時變量

68 No magic numbers

68 不要出現純數字

69 Make no assumptions about sizes

69 不要對size做假定

70 Beware of casts (C issues)

70 對類型轉換要小心(C語言問題)

71 Handle special cases directly

71 立即處理特殊情況

72 Don't try to make lint happy

72 用不着試圖讓lint不報告問題

73 Put memory allocation and deallocation code in the same place

73 配置設定記憶體和釋放記憶體應該在一起

74 Heap memory is expensive

74 堆記憶體是昂貴的

75 Test routines should not be interactive

75 測試過程不應該是互動的

76 An error message should tell the user what's right

76 錯誤資訊應該告訴使用者什麼是正确的

77 Don't print error messages if an error is recoverable

77 如果一個錯誤是可恢複的,就不要列印錯誤資訊

78 Don't use system-dependent functions for error messages

78 不要在錯誤資訊中使用系統相關的函數

繼續閱讀