C++ Coding Standards: 101 Rules, Guidelines, and Best Practices

Book description

Consistent, high-quality coding standards improve software quality, reduce time-to-market, promote teamwork, eliminate time wasted on inconsequential matters, and simplify maintenance. Now, two of the world's most respected C++ experts distill the rich collective experience of the global C++ community into a set of coding standards that every developer and development team can understand and use as a basis for their own coding standards.

The authors cover virtually every facet of C++ programming: design and coding style, functions, operators, class design, inheritance, construction/destruction, copying, assignment, namespaces, modules, templates, genericity, exceptions, STL containers and algorithms, and more. Each standard is described concisely, with practical examples. From type definition to error handling, this book presents C++ best practices, including some that have only recently been identified and standardized-techniques you may not know even if you've used C++ for years. Along the way, you'll find answers to questions like

  • What's worth standardizing--and what isn't?

  • What are the best ways to code for scalability?

  • What are the elements of a rational error handling policy?

  • How (and why) do you avoid unnecessary initialization, cyclic, and definitional dependencies?

  • When (and how) should you use static and dynamic polymorphism together?

  • How do you practice "safe" overriding?

  • When should you provide a no-fail swap?

  • Why and how should you prevent exceptions from propagating across module boundaries?

  • Why shouldn't you write namespace declarations or directives in a header file?

  • Why should you use STL vector and string instead of arrays?

  • How do you choose the right STL search or sort algorithm?

  • What rules should you follow to ensure type-safe code?

  • Whether you're working alone or with others, C++ Coding Standards will help you write cleaner code--and write it faster, with fewer hassles and less frustration.

    Table of contents

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

    Product information

    • Title: C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
    • Author(s): Herb Sutter, Andrei Alexandrescu
    • Release date: October 2004
    • Publisher(s): Addison-Wesley Professional
    • ISBN: 0321113586