設計過程
1 編程的本質是:沒有意外,最小化耦合,最大化內聚
2 根除復雜性這個惡魔(Part 1)
2.1 不要解決不存在的問題
2.2 解決一個問題,而不要解決一類問題
3 A user interface should not look like a computer program (the transparency principle)
3 用戶接口看起來不應該象一個計算機程序(透明原則)
4 Don't confuse ease of learning with ease of use
4 不要混淆易于使用和易于掌握這兩個概念
5 Productivity can be measured in the number of keystrokes
5 生產率可以通過擊鍵次數來衡量
6 If you can't say it in English, you can't say it in C/C++
6 如果你不能用自然語言表達,也就不能用C/C++表達
6.1 Do the comments first
6.1 先寫注釋
7 Read code
7 閱讀代碼
7.1 There's no room for prima donnas in a contemporary programming shop
8 Decompose complex problems into smaller tasks
8 將復雜的問題分解為多個小任務
9 Use the whole language (Use the appropriate tool for the job)
10 A problem must be thought through before it can be solved
10 解決問題之前應該深思熟慮
11 Computer programming is a service industry
11 計算機編程是服務行業
12 Involve users in the development process
12 開發過程應該有用戶參與
13 The customer is always right
13 客戶永遠是正確的
14 Small is Beautiful. (Big == slow)
14 小即是美(大==慢)
一般開發問題
15 First, do no harm
15 第一,不要有害
16 Edit your code
16 編輯你的代碼
17 A program must be written at least twice
17 一個程序應該至少寫兩次
18 You can't measure productivity by volume
18 不能用代碼量衡量生產率
19 You can't program in isolation
19 編程不能與世隔絕
20 Goof off
20 別把編程太當回事
21 Write code with maintenance in mind梩he maintenance programmer is you
21 寫代碼的時候應該時刻想著維護,假設你自己就是維護者
21.1 Efficiency is often a bugaboo
21.1 追求效率往往導致其它問題
格式化和文檔
22 Uncommented code has no value
22 沒有注釋的代碼沒有價值
23 Put the code and the documentation in the same place
23 將代碼和文檔放在一起
24 Comments should be sentences
24 注釋應該是完整的句子
25 Run your code through a spelling checker
25 你的代碼應該能通過拼寫檢查
26 A comment shouldn't restate the obvious
26 不要注釋顯而易見的東西
27 A comment should provide only information needed for maintenance
27 注釋中應該只提供對維護有幫助的內容
28 Comments should be in blocks
28 使用塊注釋
29 Comments should align vertically
29 注釋應該垂直對齊
30 Use neat columns as much as possible
30 代碼應該盡可能的整齊的分列
31 Don't put comments between the function name and the open brace
31 不要在函數名和第一個花括號之間寫注釋
32 Mark the ends of long compound statements with something reasonable
32 在較長的代碼塊之后做標記性注釋
33 Put only one statement per line
33 每行只寫一條語句
34 Put argument names in function prototypes
34 函數原型中應該寫上參數名
35 Use a 損redicate?form to split up long expressions
36 A subroutine should fit on a screen
36 一個子函數的長度應該在一屏以內
37 All code should be printable
37 所有的代碼都應該是可打印的
38 Use lines of dashes for visual separation between subroutines
38 使用整行連字符來隔離子函數代碼
39 White space is one of the most effective comments
39 空白是最好的注釋之一
40 Use four-space indents
40 使用四個空格做縮進
41 Indent statements associated with a flow-control statement
41 縮進所有與一個流程控制語句相關的語句
41.1.Comments should be at the same indent level as the surrounding code
41.1 注釋應該與其對應的代碼一同縮進
42 Align braces vertically at the outer level
42 在外層垂直對齊花括號
43 Use braces when more than one line is present under a flow-control statement
43 在流程控制語句下面如果有多于一條的語句,應該使用花括號
名字和標識
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 不要在錯誤信息中使用系統相關的函數
預處理器
79 Everything in a .h file should be used in at least two .c files
79 任何出現在頭文件的東西都應該在至少兩個.c文件中被使用
80 Use nested #includes
80 使用嵌套的#include
81 You should always be able to replace a macro with a function
81 你應該總是可以用函數代替一個宏
81.1 ?: is not the same as if/else
81.1 ?: 并不等同于if/else
81.2 Parenthesize macro bodies and arguments
81.2 在宏定義的body和參數外都應該加上()
82 enum and const are better than a macro
82 枚舉和常量都比宏好
83 A parameterized-macro argument should not appear more than once on the right-hand side
83 參數化的宏的參數不能作為右值在宏中出現一次以上
83.1 Never use macros for character constants
83.1 永遠不要用宏來表示字符常量
84 When all else fails, use the preprocessor
84 如果所有的else都失敗了,使用預處理器
C語言相關原則
85 Stamp out the demons of complexity (Part 2)
85 根除復雜性的惡魔(Part 2)
85.1 Eliminate clutter.
85.1 消除混亂
85.2 Avoid bitwise masks; use bit fields
85.2 避免位掩碼,使用位域
85.3 Don't use done flags
85.3 不要使用完成標志
85.4 Assume that your reader knows C
85.4 假設你的讀者懂C語言
85.5 Don't pretend that C supports a Boolean type (#define TRUE)
85.5 不要讓C語言假裝支持布爾類型(#define TRUE)
86 1-bit bit fields should be unsigned
86 1位的位域應該是無符號的
87 Pointers must be above the base address of an array
88 Use pointers instead of array indexes
88 使用指針而不要使用數組索引
89 Avoid goto except . . .
89 避免使用goto,除非...
OO編程/設計(C++和Java)
90 Object-oriented and 搒tructured" designs don't mix
90 不要混合面向對象的設計和結構化的設計
90.1 If it's not object-oriented, use C
90.1 如果不是面向對象的,就用C語言
91 Expect to spend more time in design and less in development
91 在設計上多花些時間,在開發上少花些時間
92 C++ class libraries usually can't be used in a naive way
92 C++類庫通常不能沒有實驗就使用
93 Use checklists
93 使用checklist
94 Messages should exercise capabilities, not request information
94 消息應該用做完成動作,而不是請求信息
95 You usually cannot convert an existing structured program to object-oriented
95 通常不能將結構化的程序轉化為面向對象的程序
96 A derived class object is a base-class object
96 派生類的對象是基類對象
97 Derivation is the process of adding member data and methods
97 派生是增加成員數據和方法的過程
98 Design the objects first
98 首先設計對象
99 Design the hierarchy next, from the bottom up
99 然后從下向上的設計類層次
99.1 Base classes should have more than one derived class
99.1 基類應該有多于一個派生類
100 The capabilities defined in the base class should be used by all derived classes
100 基類定義的功能應該能被所有派生類使用
101 C++ is not Smalltalk梐void a common object class
101 C++不是Smalltalk,應該避免公共對象類
102 Mix-ins shouldn't derive from anything in C++, in Java there's no problem if you follow the next rule:
103 Mix-ins should be C++ virtual base classes (in Java they should be interfaces)
104 Initialize virtual base classes with the default constructor
104 用缺省構造函數初始化虛基類
105 Derivation is not appropriate if you never send a base-class message to a derived-class object
105 如果沒有從基類向派生類的消息,則派生是不適合的
106 Choose containment over derivation whenever possible
107 Use private base classes only when you must provide virtual overrides (C++ only)
108 Design the data structures last
108 最后設計數據結構
109 All data in a class definition must be private
109 類中的所有數據都必須是私有的
110 Never provide public access to private data
110 從不提供對私有數據的公共訪問方法
110.1 Do not use get/set functions
110.1 不要用get/set函數
111 Give up on C idioms when coding in C++
111 在使用C++的時候放棄C語言慣用語
112 Design with derivation in mind
112 設計的時候想著派生
112.1 A member function should usually use the private data of a class
112.1 成員函數應該通常使用私有數據
113 Use const (final in Java)
113 使用常量
114 Use struct only if everything's public and there are no member functions (C++ only)
114 只在所有成員都是公有并且沒有成員函數的時候使用結構(只針對C++)
115 Don't put function bodies into class definitions (C++ only)
115 不要將函數體放到類定義中(只針對C++)
116 Avoid function overloads and default arguments
116 避免函數重載和缺省參數
117 Avoid friend classes (in Java, don't use package access.)
117 避免友元(對于Java,不要使用package access)
118 Inheritance is a form of coupling
118 繼承是耦合的形式之一
119 Don't corrupt the global name space
119 不要污染全局名字空間
C++ RulesC++原則
References引用
120 Reference arguments should always be const
120 作為參數的引用應該是const
121 Never use references as outputs, use pointers
121 不要用引用作為返回值,使用指針
122 Do not return references (or pointers) to local variables
122 不要返回局部變量的引用(或者指針)
123 Do not return references to memory that came from new
123 不要返回由new分配的內存的引用
Constructors, Destructors, and operator=()構造函數,析構函數和賦值操作符
124 Operator=() should return a const reference
124 =操作符應該返回一個const引用
125 Assignment to self must work
125 賦值給自己必須是可以的
126 Classes having pointer members should always define a copy constructor and operator=()
126 有指針類型成員的類通常應該定義拷貝構造函數和=操作符
127 If you can access an object, it has been initialized
127 如果你可以訪問一個對象,它就應該是已經被初始化了的
128 Use member-initialization lists
128 使用成員初始化列表
129 Assume that members and base classes are initialized in random order
129 假設成員和基類是按照隨機的順序被初始化的
130 Copy constructors must use member initialization lists
130 拷貝構造函數必須使用成員初始化列表
131 Derived classes should usually define a copy constructor and operator=()
131 派生類通常應該定義一個拷貝構造函數和=操作符
132 Constructors not suitable for type conversion should have two or more arguments
132 不適合進行類型轉化的構造函數應該有兩個以上的參數
133 Use instance counts for class-level initialization
133 在class級的初始化中使用實例計數
134 Avoid two-part initialization
134 避免兩部分初始化
135 C++ wrappers around existing interfaces rarely work well
Virtual Functions虛函數
136 Virtual functions are those functions that you can't write at the base-class level
136 虛函數是那種不能在基類實現的函數
137 A virtual function isn't virtual when called from a constructor or destructor
137 在構造或者析構函數中,虛函數會失去virtual的特性
138 Do not call pure virtual functions from constructors
138 不要在構造函數中調用純虛函數
139 Destructors should always be virtual
139 析構函數應該總是virtual的
140 Base-class functions that have the same name as derived-class functions generally should be virtual
140 基類中于派生類同名的函數通常應該是virtual的
141 Don't make a function virtual unless you want the derived class to get control of it
141 不要將函數定義為virtual,除非你希望派生類可以控制它
142 protected functions should usually be virtual
142 保護的成員函數通常都應該是virtual的
143 Beware of casts: C++ issues
143 小心類型轉換:C++問題
144 Don't call constructors from operator=()
144 不要在=操作符中調用構造函數
Operator Overloads操作符重載
145 An operator is an abbreviation (no surprises)
145 操作符實際上是函數的縮寫方式(不要驚訝)
146 Use operator overloads only to define operations for which there is a C analog (no surprises)
146 只有在存在一個C語言相似物的時候,才應該重載操作符
147 Once you overload an operation, you must overload all similar operations
147 一旦你重載了一個操作,你必須重載所有相似的操作
148 Operator overloads should work exactly like they would in C
148 操作符重載后必須可以象在C中一樣使用
149 It's best for a binary-operator overload to be an inline alias for a cast
150 Don't go bonkers with type-conversion operators
150 不要過多的使用類型轉換操作符
151 Do all type conversions with constructors if possible
151 盡量在構造函數中完成類型轉換
Memory Management
152 Use new/delete rather than malloc()/free()
152 使用new/delete而不要使用malloc()/free()
153 All memory allocated in a constructor should be freed in the destructor
153 所有在構造函數中分配的內存都應該在析構函數中釋放
154 Local overloads of new and delete are dangerous
154 重載局部的new/delete操作符是非常危險的
Templates模版
155 Use inline function templates instead of parameterized macros
155 使用inline函數模版而不要用參數化的宏
156 Always be aware of the size of the expanded template
156 總是注意擴展模版的大小
157 Class templates should usually define derived classes
157 類模版通常應該定義派生類
158 Templates do not replace derivation; they automate it
158 模版不能代替繼承,它實際上是自動繼承的
Exceptions
159 Intend for exceptions not to be caught
159
160 Throw error objects when possible
160 如果可能,盡量拋出錯誤對象
161 Throwing exceptions from constructors is tricky
161 可以從構造函數中拋出異常