Bibliographic record and links to related information available from the Library of Congress catalog.
Note: Contents data are machine generated based on pre-publication provided by the publisher. Contents may have variations from the printed book or be incomplete or contain other coding.
Contents Preface xi Organizational and Policy Issues 16 0. Don't sweat the small stuff. (Or: Know what not to standardize.) 17 1. Compile cleanly at high warning levels. 19 2. Use an automated build system. 22 3. Use a version control system. 23 4. Invest in code reviews. 24 Design Style 25 5. Give one entity one cohesive responsibility. 26 6. Clarity, simplicity, and correctness come first. 27 7. Know when and how to code for scalability. 28 8. Don't prematurely optimize. 30 9. Don't prematurely pessimize. 32 10. Minimize global and shared data. 33 11. Hide information. 34 12. Know when and how to code for concurrency. 35 13. Ensure resources are owned by objects. Use explicit RAII and smart pointers. 38 Coding Style 41 14. Prefer compile- and link-time errors to run-time errors. 42 15. Use const proactively. 44 16. Avoid macros. 46 17. Avoid magic numbers. 48 viii Contents 18. Declare variables as locally as possible. 49 19. Always initialize variables. 50 20. Avoid long functions. Avoid deep nesting. 52 21. Avoid initialization dependencies across compilation units. 53 22. Minimize definitional dependencies. Avoid cyclic dependencies. 54 23. Make header files self-sufficient. 56 24. Always write internal #include guards. Never write external #include guards. 57 Functions and Operators 59 25. Take parameters appropriately by value, (smart) pointer, or reference. 60 26. Preserve natural semantics for overloaded operators. 61 27. Prefer the canonical forms of arithmetic and assignment operators. 62 28. Prefer the canonical form of ++ and --. Prefer calling the prefix forms. 64 29. Consider overloading to avoid implicit type conversions. 65 30. Avoid overloading &&, ||, or , (comma) . 66 31. Don't write code that depends on the order of evaluation of function arguments. 68 Class Design and Inheritance 69 32. Be clear what kind of class you're writing. 70 33. Prefer minimal classes to monolithic classes. 71 34. Prefer composition to inheritance. 72 35. Avoid inheriting from classes that were not designed to be base classes. 74 36. Prefer providing abstract interfaces. 76 37. The "what" of public inheritance: Public inheritance is substitutability. 78 38. Practice safe overriding. 80 39. Consider making virtual functions nonpublic, and public functions nonvirtual. 82 40. Avoid providing implicit conversions. 84 41. Make data members private, except in behaviorless aggregates (C-style structs). 86 42. Don't give away your internals. 88 43. Pimpl judiciously. 90 44. Prefer writing nonmember nonfriend functions. 93 45. Always provide new and delete together. 94 46. If you provide any class-specific new, provide all of the standard forms (plain, in-place, and nothrow). 96 Construction, Destruction, and Copying 99 47. Define and initialize member variables in the same order. 100 48. Prefer initialization to assignment in constructors. 101 49. Avoid calling virtual functions in constructors and destructors. 102 50. Make base class destructors public and virtual, or protected and nonvirtual. 104 51. Destructors, deallocation, and swap never fail. 106 52. Copy and destroy consistently. 108 53. Explicitly enable or disable copying. 109 54. Avoid slicing. Consider Clone instead of copying in base classes. 110 55. Prefer the canonical form of assignment. 113 56. Whenever it makes sense, provide a no-fail swap (and provide it correctly). 114 Namespaces and Modules 117 57. Keep a type and its nonmember function interface in the same namespace. 118 58. Keep types and functions in separate namespaces unless they're specifically intended to work together. 120 59. Don't write namespace usings in a header file or before an #include. 122 60. Avoid allocating and deallocating memory in different modules. 125 61. Don't define entities with linkage in a header file. 126 62. Don't allow exceptions to propagate across module boundaries. 128 63. Use sufficiently portable types in a module's interface. 130 Templates and Genericity 133 64. Blend static and dynamic polymorphism judiciously. 134 65. Customize intentionally and explicitly. 136 66. Don't specialize function templates. 140 67. Don't write unintentionally non-generic code. 142 Error Handling and Exceptions 143 68. Assert liberally to document internal assumptions and invariants. 144 69. Establish a rational error handling policy, and follow it strictly. 146 70. Distinguish between errors and non-errors. 148 71. Design and write error-safe code. 151 72. Prefer to use exceptions to report errors. 154 73. Throw by value, catch by reference. 158 74. Report, handle, and translate errors appropriately. 159 75. Avoid exception specifications. 160 STL: Containers 163 76. Use vector by default. Otherwise, choose an appropriate container. 164 77. Use vector and string instead of arrays. 166 78. Use vector (and string::c_str) to exchange data with non-C++ APIs. 167 79. Store only values and smart pointers in containers. 168 80. Prefer push_back to other ways of expanding a sequence. 169 81. Prefer range operations to single-element operations. 170 82. Use the accepted idioms to really shrink capacity and really erase elements. 171 STL: Algorithms 173 83. Use a checked STL implementation. 174 84. Prefer algorithm calls to handwritten loops. 176 85. Use the right STL search algorithm. 179 86. Use the right STL sort algorithm. 180 87. Make predicates pure functions. 182 88. Prefer function objects over functions as algorithm and comparer arguments. 184 89. Write function objects correctly. 186 Type Safety 187 90. Avoid type switching; prefer polymorphism. 188 91. Rely on types, not on representations. 190 92. Avoid using reinterpret_cast. 191 93. Avoid using static_cast on pointers. 192 94. Avoid casting away const. 193 95. Don't use C-style casts. 194 96. Don't memcpy or memcmp non-PODs. 196 97. Don't use unions to reinterpret representation. 197 98. Don't use varargs (ellipsis). 198 99. Don't use invalid objects. Don't use unsafe functions. 199 100. Don't treat arrays polymorphically. 200 Bibliography 201 Summary of Summaries 209 Index 221
Library of Congress Subject Headings for this publication:
C++ (Computer program language).