預覽 Herb Sutter 及 Andrei Alexandrescu 的新書《C++ Coding Standards》。
還沒有機會去書店買,但從 Amazon.com 裡的評論看來,這本書與 Effective C++ 似乎頗有互補作用。
- Don’t sweat the small stuff. (Or: Know what not to standardize.) 2
- Compile cleanly at high warning levels. 4
- Use an automated build system. 7
- Use a version control system. 8
- Invest in code reviews. 9
- Give one entity one cohesive responsibility. 12
- Correctness, simplicity, and clarity come first. 13
- Know when and how to code for scalability. 14
- Don’t optimize prematurely. 16
- Don’t pessimize prematurely. 18
- Minimize global and shared data. 19
- Hide information. 20
- Know when and how to code for concurrency. 21
- Ensure resources are owned by objects. Use explicit RAII and smart pointers. 24
- Prefer compile- and link-time errors to run-time errors. 28
- Use const proactively. 30
- Avoid macros. 32
- Avoid magic numbers. 34
- Declare variables as locally as possible. 35
- Always initialize variables. 36
- Avoid long functions. Avoid deep nesting. 38
- Avoid initialization dependencies across compilation units. 39
- Minimize definitional dependencies. Avoid cyclic dependencies. 40
- Make header files self-sufficient. 42
- Always write internal #include guards. Never write external #include guards. 43
- Take parameters appropriately by value, (smart) pointer, or reference. 46
- Preserve natural semantics for overloaded operators. 47
- Prefer the canonical forms of arithmetic and assignment operators. 48
- Prefer the canonical form of ++ and –. Prefer calling the prefix forms. 50
- Consider overloading to avoid implicit type conversions. 51
- Avoid overloading &&, ||, or , (comma). 52
- Don’t write code that depends on the order of evaluation of function arguments. 54
- Be clear what kind of class you’re writing. 56
- Prefer minimal classes to monolithic classes. 57
- Prefer composition to inheritance. 58
- Avoid inheriting from classes that were not designed to be base classes. 60
- Prefer providing abstract interfaces. 62
- Public inheritance is substitutability. Inherit, not to reuse, but to be reused. 64
- Practice safe overriding. 66
- Consider making virtual functions nonpublic, and public functions nonvirtual. 68
- Avoid providing implicit conversions. 70
- Make data members private, except in behaviorless aggregates (C-style structs). 72
- Don’t give away your internals. 74
- Pimpl judiciously. 76
- Prefer writing nonmember nonfriend functions. 79
- Always provide new and delete together. 80
- If you provide any class-specific new, provide all of the standard forms (plain, in-place, and nothrow). 82
- Define and initialize member variables in the same order. 86
- Prefer initialization to assignment in constructors. 87
- Avoid calling virtual functions in constructors and destructors. 88
- Make base class destructors public and virtual, or protected and nonvirtual. 90
- Destructors, deallocation, and swap never fail. 92
- Copy and destroy consistently. 94
- Explicitly enable or disable copying. 95
- Avoid slicing. Consider Clone instead of copying in base classes. 96
- Prefer the canonical form of assignment. 99
- Whenever it makes sense, provide a no-fail swap (and provide it correctly). 100
- Keep a type and its nonmember function interface in the same namespace. 104
- Keep types and functions in separate namespaces unless they’re specifically intended to work together. 106
- Don’t write namespace usings in a header file or before an #include. 108
- Avoid allocating and deallocating memory in different modules. 111
- Don’t define entities with linkage in a header file. 112
- Don’t allow exceptions to propagate across module boundaries. 114
- Use sufficiently portable types in a module’s interface. 116
- Blend static and dynamic polymorphism judiciously. 120
- Customize intentionally and explicitly. 122
- Don’t specialize function templates. 126
- Don’t write unintentionally nongeneric code. 128
- Assert liberally to document internal assumptions and invariants. 130
- Establish a rational error handling policy, and follow it strictly. 132
- Distinguish between errors and non-errors. 134
- Design and write error-safe code. 137
- Prefer to use exceptions to report errors. 140
- Throw by value, catch by reference. 144
- Report, handle, and translate errors appropriately. 145
- Avoid exception specifications. 146
- Use vector by default. Otherwise, choose an appropriate container. 150
- Use vector and string instead of arrays. 152
- Use vector (and string::c_str) to exchange data with non-C++ APIs. 153
- Store only values and smart pointers in containers. 154
- Prefer push_back to other ways of expanding a sequence. 155
- Prefer range operations to single-element operations. 156
- Use the accepted idioms to really shrink capacity and really erase elements. 157
- Use a checked STL implementation. 160
- Prefer algorithm calls to handwritten loops. 162
- Use the right STL search algorithm. 165
- Use the right STL sort algorithm. 166
- Make predicates pure functions. 168
- Prefer function objects over functions as algorithm and comparer arguments. 170
- Write function objects correctly. 172
- Avoid type switching; prefer polymorphism. 174
- Rely on types, not on representations. 176
- Avoid using reinterpret_cast. 177
- Avoid using static_cast on pointers. 178
- Avoid casting away const. 179
- Don’t use C-style casts. 180
- Don’t memcpy or memcmp non-PODs. 182
- Don’t use unions to reinterpret representation. 183
- Don’t use varargs (ellipsis). 184
- Don’t use invalid objects. Don’t use unsafe functions. 185
- Don’t treat arrays polymorphically. 186