設(shè)計(jì)過(guò)程
1 編程的本質(zhì)是:沒(méi)有意外,最小化耦合,最大化內(nèi)聚
2 根除復(fù)雜性這個(gè)惡魔(Part 1)
2.1 不要解決不存在的問(wèn)題
2.2 解決一個(gè)問(wèn)題,而不要解決一類問(wèn)題
3 A user interface should not look like a computer program (the transparency principle)
3 用戶接口看起來(lái)不應(yīng)該象一個(gè)計(jì)算機(jī)程序(透明原則)
4 Don't confuse ease of learning with ease of use
4 不要混淆易于使用和易于掌握這兩個(gè)概念
5 Productivity can be measured in the number of keystrokes
5 生產(chǎn)率可以通過(guò)擊鍵次數(shù)來(lái)衡量
6 If you can't say it in English, you can't say it in C/C++
6 如果你不能用自然語(yǔ)言表達(dá),也就不能用C/C++表達(dá)
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 將復(fù)雜的問(wèn)題分解為多個(gè)小任務(wù)
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 解決問(wèn)題之前應(yīng)該深思熟慮
11 Computer programming is a service industry
11 計(jì)算機(jī)編程是服務(wù)行業(yè)
12 Involve users in the development process
12 開(kāi)發(fā)過(guò)程應(yīng)該有用戶參與
13 The customer is always right
13 客戶永遠(yuǎn)是正確的
14 Small is Beautiful. (Big == slow)
14 小即是美(大==慢)
一般開(kāi)發(fā)問(wèn)題
15 First, do no harm
15 第一,不要有害
16 Edit your code
16 編輯你的代碼
17 A program must be written at least twice
17 一個(gè)程序應(yīng)該至少寫兩次
18 You can't measure productivity by volume
18 不能用代碼量衡量生產(chǎn)率
19 You can't program in isolation
19 編程不能與世隔絕
20 Goof off
20 別把編程太當(dāng)回事
21 Write code with maintenance in mind梩he maintenance programmer is you
21 寫代碼的時(shí)候應(yīng)該時(shí)刻想著維護(hù),假設(shè)你自己就是維護(hù)者
21.1 Efficiency is often a bugaboo
21.1 追求效率往往導(dǎo)致其它問(wèn)題
格式化和文檔
22 Uncommented code has no value
22 沒(méi)有注釋的代碼沒(méi)有價(jià)值
23 Put the code and the documentation in the same place
23 將代碼和文檔放在一起
24 Comments should be sentences
24 注釋應(yīng)該是完整的句子
25 Run your code through a spelling checker
25 你的代碼應(yīng)該能通過(guò)拼寫檢查
26 A comment shouldn't restate the obvious
26 不要注釋顯而易見(jiàn)的東西
27 A comment should provide only information needed for maintenance
27 注釋中應(yīng)該只提供對(duì)維護(hù)有幫助的內(nèi)容
28 Comments should be in blocks
28 使用塊注釋
29 Comments should align vertically
29 注釋應(yīng)該垂直對(duì)齊
30 Use neat columns as much as possible
30 代碼應(yīng)該盡可能的整齊的分列
31 Don't put comments between the function name and the open brace
31 不要在函數(shù)名和第一個(gè)花括號(hào)之間寫注釋
32 Mark the ends of long compound statements with something reasonable
32 在較長(zhǎng)的代碼塊之后做標(biāo)記性注釋
33 Put only one statement per line
33 每行只寫一條語(yǔ)句
34 Put argument names in function prototypes
34 函數(shù)原型中應(yīng)該寫上參數(shù)名
35 Use a 損redicate?form to split up long expressions
36 A subroutine should fit on a screen
36 一個(gè)子函數(shù)的長(zhǎng)度應(yīng)該在一屏以內(nèi)
37 All code should be printable
37 所有的代碼都應(yīng)該是可打印的
38 Use lines of dashes for visual separation between subroutines
38 使用整行連字符來(lái)隔離子函數(shù)代碼
39 White space is one of the most effective comments
39 空白是最好的注釋之一
40 Use four-space indents
40 使用四個(gè)空格做縮進(jìn)
41 Indent statements associated with a flow-control statement
41 縮進(jìn)所有與一個(gè)流程控制語(yǔ)句相關(guān)的語(yǔ)句
41.1.Comments should be at the same indent level as the surrounding code
41.1 注釋應(yīng)該與其對(duì)應(yīng)的代碼一同縮進(jìn)
42 Align braces vertically at the outer level
42 在外層垂直對(duì)齊花括號(hào)
43 Use braces when more than one line is present under a flow-control statement
43 在流程控制語(yǔ)句下面如果有多于一條的語(yǔ)句,應(yīng)該使用花括號(hào)
名字和標(biāo)識(shí)
44 Names should be common English words, descriptive of what the function, argument, or
variable does
44 名字應(yīng)該是普通英文單詞,用來(lái)描述這個(gè)函數(shù)、參數(shù)或者變量的作用
44.1.Do not clutter names with gibberish
44.1 不要亂起名字(無(wú)意義的,深?yuàn)W的,不貼切的)
45 Macro names should be ENTIRELY_CAPITALIZED
45 宏名字應(yīng)該形如: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避免不必要的符號(hào)
49 Symbolic constants for Boolean values are rarely necessary
49 布爾型符號(hào)常量基本上沒(méi)有用
一般編程原則
50 Don't confuse familiarity with readability
50 熟悉代碼與代碼的可讀性好是兩回事
51 A function should do only one thing
51 一個(gè)函數(shù)只應(yīng)該完成一件事
52 Too many levels of abstraction or encapsulation are as bad as too few
52 過(guò)多或者過(guò)少的抽象或者封裝層次都不好
53 A function should be called more than once, but?br>
53 一個(gè)函數(shù)應(yīng)該被多處調(diào)用
53.1 Code used more than once should be put into a function
53.1 在多于一處使用的代碼應(yīng)該變成一個(gè)函數(shù)
54 A function should have only one exit point
54 一個(gè)函數(shù)應(yīng)該只有一個(gè)退出點(diǎn)
54.1 Always put a return at the outer level
54.1 總是在外層放一個(gè)return
55 Avoid duplication of effort
55 避免費(fèi)兩遍事的事情
56 Don't corrupt the global name space
56 不要污染全局名字空間
56.1 Avoid global symbols
56.1 避免全局符號(hào)
56.2 Never require initialization of a global variable to call a function
56.2 對(duì)函數(shù)的調(diào)用不應(yīng)該依賴全局變量是否被初始化
56.2.1 Make locals static in recursive functions if the value doesn't span a recursive call
56.2.1 在第歸函數(shù)中,如果一個(gè)變量與第歸調(diào)用無(wú)關(guān),則其應(yīng)該是局部靜態(tài)變量
56.3 Use instance counts in place of initialization functions
56.3 在初始化函數(shù)中使用實(shí)例計(jì)數(shù)
56.4 If an if ends in return, don't use else
56.4 如果if語(yǔ)句中有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 盡量將運(yùn)行時(shí)刻的錯(cuò)誤提前到編譯時(shí)刻
59 Use C function pointers as selectors
59 使用C 函數(shù)指針作為選擇器
60 Avoid do/while loops
60 避免do/while循環(huán)
60.1 Never use a do/while for a forever loop
60.1 不要用do/while實(shí)現(xiàn)無(wú)限循環(huán)
61 Counting loops should count down if possible
61 循環(huán)計(jì)數(shù)應(yīng)該從大到小
62 Don't do the same thing in two ways at the same time
62 在同一時(shí)間不要用不同的方法完成同一件事
63 Use for if any two of an initialization, test, or increment are present
63 如果初始化、測(cè)試、增量三者有兩個(gè),就應(yīng)該用for語(yǔ)句
64 If it doesn't appear in the test, it shouldn't appear in the other parts of for statement
64 沒(méi)有在for語(yǔ)句的測(cè)試部分出現(xiàn)的變量,也不應(yīng)該出現(xiàn)在其他兩個(gè)部分
65 Assume that things will go wrong
65 總是假定事情要出錯(cuò)
66 Computers do not know mathematics
66 計(jì)算機(jī)不懂得數(shù)學(xué)
66.1 Expect the impossible
66.1 總是會(huì)出現(xiàn)不可能的事情
66.2 Always check error-return codes
66.2 檢查錯(cuò)誤返回代碼
67 Avoid explicit temporary variables
67 避免顯式臨時(shí)變量
68 No magic numbers
68 不要出現(xiàn)純數(shù)字
69 Make no assumptions about sizes
69 不要對(duì)size做假定
70 Beware of casts (C issues)
70 對(duì)類型轉(zhuǎn)換要小心(C語(yǔ)言問(wèn)題)
71 Handle special cases directly
71 立即處理特殊情況
72 Don't try to make lint happy
72 用不著試圖讓lint不報(bào)告問(wèn)題
73 Put memory allocation and deallocation code in the same place
73 分配內(nèi)存和釋放內(nèi)存應(yīng)該在一起
74 Heap memory is expensive
74 堆內(nèi)存是昂貴的
75 Test routines should not be interactive
75 測(cè)試過(guò)程不應(yīng)該是交互的
76 An error message should tell the user what's right
76 錯(cuò)誤信息應(yīng)該告訴用戶什么是正確的
77 Don't print error messages if an error is recoverable
77 如果一個(gè)錯(cuò)誤是可恢復(fù)的,就不要打印錯(cuò)誤信息
78 Don't use system-dependent functions for error messages
78 不要在錯(cuò)誤信息中使用系統(tǒng)相關(guān)的函數(shù)
預(yù)處理器
79 Everything in a .h file should be used in at least two .c files
79 任何出現(xiàn)在頭文件的東西都應(yīng)該在至少兩個(gè).c文件中被使用
80 Use nested #includes
80 使用嵌套的#include
81 You should always be able to replace a macro with a function
81 你應(yīng)該總是可以用函數(shù)代替一個(gè)宏
81.1 ?: is not the same as if/else
81.1 ?: 并不等同于if/else
81.2 Parenthesize macro bodies and arguments
81.2 在宏定義的body和參數(shù)外都應(yīng)該加上()
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 參數(shù)化的宏的參數(shù)不能作為右值在宏中出現(xiàn)一次以上
83.1 Never use macros for character constants
83.1 永遠(yuǎn)不要用宏來(lái)表示字符常量
84 When all else fails, use the preprocessor
84 如果所有的else都失敗了,使用預(yù)處理器
C語(yǔ)言相關(guān)原則
85 Stamp out the demons of complexity (Part 2)
85 根除復(fù)雜性的惡魔(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 不要使用完成標(biāo)志
85.4 Assume that your reader knows C
85.4 假設(shè)你的讀者懂C語(yǔ)言
85.5 Don't pretend that C supports a Boolean type (#define TRUE)
85.5 不要讓C語(yǔ)言假裝支持布爾類型(#define TRUE)
86 1-bit bit fields should be unsigned
86 1位的位域應(yīng)該是無(wú)符號(hào)的
87 Pointers must be above the base address of an array
88 Use pointers instead of array indexes
88 使用指針而不要使用數(shù)組索引
89 Avoid goto except . . .
89 避免使用goto,除非...
OO編程/設(shè)計(jì)(C++和Java)
90 Object-oriented and 搒tructured" designs don't mix
90 不要混合面向?qū)ο蟮脑O(shè)計(jì)和結(jié)構(gòu)化的設(shè)計(jì)
90.1 If it's not object-oriented, use C
90.1 如果不是面向?qū)ο蟮?,就用C語(yǔ)言
91 Expect to spend more time in design and less in development
91 在設(shè)計(jì)上多花些時(shí)間,在開(kāi)發(fā)上少花些時(shí)間
92 C++ class libraries usually can't be used in a naive way
92 C++類庫(kù)通常不能沒(méi)有實(shí)驗(yàn)就使用
93 Use checklists
93 使用checklist
94 Messages should exercise capabilities, not request information
94 消息應(yīng)該用做完成動(dòng)作,而不是請(qǐng)求信息
95 You usually cannot convert an existing structured program to object-oriented
95 通常不能將結(jié)構(gòu)化的程序轉(zhuǎn)化為面向?qū)ο蟮某绦?br />96 A derived class object is a base-class object
96 派生類的對(duì)象是基類對(duì)象
97 Derivation is the process of adding member data and methods
97 派生是增加成員數(shù)據(jù)和方法的過(guò)程
98 Design the objects first
98 首先設(shè)計(jì)對(duì)象
99 Design the hierarchy next, from the bottom up
99 然后從下向上的設(shè)計(jì)類層次
99.1 Base classes should have more than one derived class
99.1 基類應(yīng)該有多于一個(gè)派生類
100 The capabilities defined in the base class should be used by all derived classes
100 基類定義的功能應(yīng)該能被所有派生類使用
101 C++ is not Smalltalk梐void a common object class
101 C++不是Smalltalk,應(yīng)該避免公共對(duì)象類
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 用缺省構(gòu)造函數(shù)初始化虛基類
105 Derivation is not appropriate if you never send a base-class message to a derived-class object
105 如果沒(méi)有從基類向派生類的消息,則派生是不適合的
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 最后設(shè)計(jì)數(shù)據(jù)結(jié)構(gòu)
109 All data in a class definition must be private
109 類中的所有數(shù)據(jù)都必須是私有的
110 Never provide public access to private data
110 從不提供對(duì)私有數(shù)據(jù)的公共訪問(wèn)方法
110.1 Do not use get/set functions
110.1 不要用get/set函數(shù)
111 Give up on C idioms when coding in C++
111 在使用C++的時(shí)候放棄C語(yǔ)言慣用語(yǔ)
112 Design with derivation in mind
112 設(shè)計(jì)的時(shí)候想著派生
112.1 A member function should usually use the private data of a class
112.1 成員函數(shù)應(yīng)該通常使用私有數(shù)據(jù)
113 Use const (final in Java)
113 使用常量
114 Use struct only if everything's public and there are no member functions (C++ only)
114 只在所有成員都是公有并且沒(méi)有成員函數(shù)的時(shí)候使用結(jié)構(gòu)(只針對(duì)C++)
115 Don't put function bodies into class definitions (C++ only)
115 不要將函數(shù)體放到類定義中(只針對(duì)C++)
116 Avoid function overloads and default arguments
116 避免函數(shù)重載和缺省參數(shù)
117 Avoid friend classes (in Java, don't use package access.)
117 避免友元(對(duì)于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 作為參數(shù)的引用應(yīng)該是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分配的內(nèi)存的引用
Constructors, Destructors, and operator=()構(gòu)造函數(shù),析構(gòu)函數(shù)和賦值操作符
124 Operator=() should return a const reference
124 =操作符應(yīng)該返回一個(gè)const引用
125 Assignment to self must work
125 賦值給自己必須是可以的
126 Classes having pointer members should always define a copy constructor and operator=()
126 有指針類型成員的類通常應(yīng)該定義拷貝構(gòu)造函數(shù)和=操作符
127 If you can access an object, it has been initialized
127 如果你可以訪問(wèn)一個(gè)對(duì)象,它就應(yīng)該是已經(jīng)被初始化了的
128 Use member-initialization lists
128 使用成員初始化列表
129 Assume that members and base classes are initialized in random order
129 假設(shè)成員和基類是按照隨機(jī)的順序被初始化的
130 Copy constructors must use member initialization lists
130 拷貝構(gòu)造函數(shù)必須使用成員初始化列表
131 Derived classes should usually define a copy constructor and operator=()
131 派生類通常應(yīng)該定義一個(gè)拷貝構(gòu)造函數(shù)和=操作符
132 Constructors not suitable for type conversion should have two or more arguments
132 不適合進(jìn)行類型轉(zhuǎn)化的構(gòu)造函數(shù)應(yīng)該有兩個(gè)以上的參數(shù)
133 Use instance counts for class-level initialization
133 在class級(jí)的初始化中使用實(shí)例計(jì)數(shù)
134 Avoid two-part initialization
134 避免兩部分初始化
135 C++ wrappers around existing interfaces rarely work well
Virtual Functions虛函數(shù)
136 Virtual functions are those functions that you can't write at the base-class level
136 虛函數(shù)是那種不能在基類實(shí)現(xiàn)的函數(shù)
137 A virtual function isn't virtual when called from a constructor or destructor
137 在構(gòu)造或者析構(gòu)函數(shù)中,虛函數(shù)會(huì)失去virtual的特性
138 Do not call pure virtual functions from constructors
138 不要在構(gòu)造函數(shù)中調(diào)用純虛函數(shù)
139 Destructors should always be virtual
139 析構(gòu)函數(shù)應(yīng)該總是virtual的
140 Base-class functions that have the same name as derived-class functions generally should be virtual
140 基類中于派生類同名的函數(shù)通常應(yīng)該是virtual的
141 Don't make a function virtual unless you want the derived class to get control of it
141 不要將函數(shù)定義為virtual,除非你希望派生類可以控制它
142 protected functions should usually be virtual
142 保護(hù)的成員函數(shù)通常都應(yīng)該是virtual的
143 Beware of casts: C++ issues
143 小心類型轉(zhuǎn)換:C++問(wèn)題
144 Don't call constructors from operator=()
144 不要在=操作符中調(diào)用構(gòu)造函數(shù)
Operator Overloads操作符重載
145 An operator is an abbreviation (no surprises)
145 操作符實(shí)際上是函數(shù)的縮寫方式(不要驚訝)
146 Use operator overloads only to define operations for which there is a C analog (no surprises)
146 只有在存在一個(gè)C語(yǔ)言相似物的時(shí)候,才應(yīng)該重載操作符
147 Once you overload an operation, you must overload all similar operations
147 一旦你重載了一個(gè)操作,你必須重載所有相似的操作
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 不要過(guò)多的使用類型轉(zhuǎn)換操作符
151 Do all type conversions with constructors if possible
151 盡量在構(gòu)造函數(shù)中完成類型轉(zhuǎn)換
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 所有在構(gòu)造函數(shù)中分配的內(nèi)存都應(yīng)該在析構(gòu)函數(shù)中釋放
154 Local overloads of new and delete are dangerous
154 重載局部的new/delete操作符是非常危險(xiǎn)的
Templates模版
155 Use inline function templates instead of parameterized macros
155 使用inline函數(shù)模版而不要用參數(shù)化的宏
156 Always be aware of the size of the expanded template
156 總是注意擴(kuò)展模版的大小
157 Class templates should usually define derived classes
157 類模版通常應(yīng)該定義派生類
158 Templates do not replace derivation; they automate it
158 模版不能代替繼承,它實(shí)際上是自動(dòng)繼承的
Exceptions
159 Intend for exceptions not to be caught
159
160 Throw error objects when possible
160 如果可能,盡量拋出錯(cuò)誤對(duì)象
161 Throwing exceptions from constructors is tricky
161 可以從構(gòu)造函數(shù)中拋出異常