Cpp Coding Standards/GEN
Topic-Id: GEN
General advice for coding.
Contents
Summary
Const Correctness (Const)
Make every member function const that does not change the logical state of its class. Use “mutable” to distinguish logical from physical state.
Prefer constants over variables wherever possible.
Resource Allocation is Initialization (RAII)
Use the “Resource allocation is initialization” (RAII) idiom to manage resources.
Use smart pointers for objects on the heap.
Avoid Duplicate Code (Dupli)
Avoid and remove dupliate code. Duplicate code may be okay, if otherwise dependencies among so far unrelated modules would be created. Among constructors of the same class some code duplication is okay.
Initialize Everything Immediately (Init)
Initialize all variables at declaration.
Every struct needs a constructor.
Preferably mention every data member, including the default constructed ones, in the constructor's initialization section.
Optimize Later (Opti)
Do not optimize before you really know there is a performance problem. Use inline sparingly. However do optimize in algorithm complexity.
Clear Origin of Local Data (LocalData)
Use a consistent way to distinguish member data, local variables and function parameters.
Local Conventions (LocalConv)
When changing code in a module, follow local naming conventions.
Matching Array Delete (ArrayDel)
When using new[], employ a matching delete[] statement.
Related Rules
- OBSOLETE:Types - Preferred Types