NALCG Coding Standards

Portablity

  1. All code must be cross platform, ANSI C++. No dependencies on platform-specific headers and / or types are allowed (the only exception is when dealing with platform-specific features like windowing, which must be implemented for each platform separately).
  2. If you serialise / deserialise any data, subclass from Serializer and use its methods, it will insulate you from endianness issues. If you need to serialise any types it doesn't already handle, make sure you deal with endianness issues in the same way Serializer does (ie use native endianness by default but detect the inverse on reading and perform flipping if required).

C++ Standards compliance

  1. Always prefer the STL over custom containers / algorithms.
  2. Always prefer C++ techniques over C.
  3. Use the PImpl idiom to reduce dependencies between classes.
  4. Always use const-correctness. Methods taking non-primitive types as parameters should generally take them as const references, methods returning non-primitive types should generally return them as const references. Declare all methods that do not modify internal state 'const'. For lazy-update getter methods, declare the internal state which is lazy-updated 'mutable'.
  5. Prefer 'protected' over 'private' to encourage specialisation where appropriate
  6. Always declare destructors 'virtual' unless the class you are writing should not have any vtable (no other virtual methods).
  7. Avoid non-const by-ref parameters unless you have no other option. We prefer not to have in/our parameters since they are less intuitive.

Naming conventions & Documentation

  1. Classes, types and structures must be title case (MyNewClass).
  2. Methods and local variables must be camel case (myNewMethod).
  3. Member variables should be prefixed with 'm' (mInstanceVar), static member variables should be prefixed 'ms' (msStaticMemberVar). Do not use any other prefixing such as Hungarian notation.
  4. Enums should be named in title case, enum values should be all upper case
  5. Use verbose, descriptive names for classes, methods, variables - everything except trival counters. Code should be self-describing, don't be obtuse.

Memory Management

  1. Avoid using new/delete directly. 
    1. Use boost::shared_ptr instead
  2. Don't leak :(

Style issues

  1. Indent with Spaces only, single indentation level is four (4) spaces. Tabs shall not be used anywhere else, but in the Makefile
  2. Insert a newline before an open brace (contentious I know!)
  3. Always insert spaces in between operators and operands (x + y, not x+y)
  4. Use parenthesis to make the operator precedence unambiguous, even when it is not required ((x * y) + 1, not x * y + 1)

Error handling

  1. Fatal errors should always be dealt with though exception handling. No return-value error reporting.
  2. Whenever you make an assumption in your code, verify it with an assert().

Design issues

  1. Use existing design patterns and identify them by their well known names. A good starting reference is the 'Gang of Four' book.
  2. Use strong encapsulation. Top-level interfaces should hide implementations and not require the user of the library to understand internals. Avoid public attributes except in structs.
  3. Don't use 'friend' if you can avoid it. Where classes need to collaborate on an internal implementation, prefix the methods they use to communicate with '_' (this is our demarcation for 'recommended for internal use only'). This can also be used to expose advanced functionality only intended for very skilled users.

Final words

If in doubt, do as the existing code does!