Coding Rules and Guidelines
C++ should conform to the ANSI C++ standard draft :
http://www.kuzbass.ru:8086/docs/isocpp/
Have also a look to the
Ogre project recommendations :
http://temas.obelisk.net/ogre/CR/docs/howto.html
General Guidelines
- Provide plenty of comments (in English, Doxygen compliant)
- All code should be also written in English
- Don't reinvent the wheel, use clever dependencies!
- Use only open sourced projects as dependencies
- Avoid platform dependant stuff as possible
- Must compile at least on Debian ( GCC 4) and Windows 32bit platform (VC++2005) as Ogre do
- Avoid DirectX? or OpenGL? dependant routine (use Ogre!)
- Code for clarity rather than efficiency
- Code defensively, check return codes and don't assume success
- Use meaningful names (not i, ii, iii and j)
- Indent nested blocks
- Avoid implicit data-type conversions, use explicit conversions
- Avoid complicated "if"' constructs
- Avoid global variables as possible
Rules for C++ code
- C/C++ include files have the extension .h
- C++ source files have the extension .cpp
- C source files have the extension .c
- Use a separate .cpp and corresponding .h file for each C++ class. The filename should be identical to the class name
- Do not create classes/files whose names differ only by case
- Data members should have names beginning with a leading 'm'
- Prefer standard C++ library and STL functions over C stdlib functions
- Include directives should be of the form #include "<package>/file.h"
- Use existing classes where possible
- Encapsulate your global variables, constants, enumerated types and typedefs in a class
- Avoid overloading functions and operators unless it brings clear improvements
- Do not treat pointers as booleans, i.e. never implicitly compare pointers to nonzero
- Routines that do not have a void return type should always return something (i.e. no "return;")
- Preconditions and postconditions are checked with assert(<condition>)
- Long, complex in-line member functions should be avoided. Don't inline anything that calls something else, or involves significant control structures
- Use "bool", not "int", for true/false data items
- 0 should be preferred over NULL
- Use enums for small named constants within classes. Do not use #define for this because it can result in name collisions in other people code
- Use "const" whenever possible
- Never use #include when a simple forward declaration ("class foo;") will do