Home Site map Contacts CZ

Jan Ringoš, Tringi.TRIMCORE.cz

Papers & C++  »  Papers  » 

C++ Coding Standard

Coding standard and various notes regarding writing software in the C and C++ programming languages. This document describes my preferences of writing and structuring code in recent and future projects.

Important note: The listing here is only a summary of the complete document which has not yet been translated. To see the whole document, switch to the Czech language using the CZ icon at the upper right corner of the page.

General development recommendations

  • [A1] Clean compilation without any warning at –Wall and -Wextra
  • [A2] At least single test program for every logical unit
  • [A3] Methodical separating logical sets of functionality into standalone libraries
  • [A4] Use of idioms and standard patterns
  • [A5] Holy wars about GOTOs, SE-SE, etc. are forbidden!
  • [A6] Strict consistency

Conceptual rules

  • [B1] Minimal use of global or shared data
  • [B2] Single entity has a single responsibility
  • [B3] No unused (dead) or duplicate code
  • [B4] Hidden implementation, PIMPL idiom
  • [B5] Initialization is allocation, RAII idiom
  • [B6] No-fail destructor and swap operation
  • [B7] Limit functions to 200 LLOC and 7 parameters
  • [B8] Natural semantics and canonical definition of operators
  • [B9] Handling all potential points of failure

Comments

  • [C1] Don't comment WHAT, do comment WHY
  • [C2] No commentary decorations
  • [C3] File header comment
  • [C4] Contract description at every function declaration
  • [C5] TODO comments
  • [C6] Always commented empty blocks

Styling

  • [D1] 4 spaces indenting, no tabs
  • [D2] Separating with spaces, partitioning with empty rows
  • [D3] Naming [...]
  • [D4] Use of typical, common, identifier names [...]
  • [D5] Single command or expression on a single row
  • [D6] No need to conserve space

Mandatory syntactic rules

  • [E1] Digraphs and Trigraphs are forbidden
  • [E2] Restricted preprocesor use
  • [E3] Forbidden and restricted C library functions [...]
  • [E4] No magic constants
  • [E5] Prefer suitable technique and algorithm before manual optimizations
  • [E6] Const-correct code
  • [E7] Tight variables locality
  • [E8] Polymorphism instead of type codes where possible
  • [E9] Extern declarations instead of #include where easily possible
  • [E10] Restricted casting [...]
  • [E11] Explicit this qualification
  • [E12] Only local "using namespace"

References used

  1. Addison Wesley Professional, ISBN: 0-321-11358-6, October 25, 2004
    Herb Sutter, Andrei Alexandrescu: C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
  2. NO STARCH PRESS, ISBN: 1-59327-119-0, December 2006
    Pete Goodliffe: Code Craft, The Practice of Writing Excellent Code
exertion of the imagination, portfolio library and software repository