C++ code conventions

From Popms Wiki
Jump to: navigation, search

Many of the following tips is taken from the OGRE Coding Standards.


[edit] General

  • Add following license information to each of the source files:
Copyright (C) <year of the last modification> Populous Mana Source Development Team

This file is part of [name_of_the_program].

[name_of_the_program] is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

[name_of_the_program] is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with [name_of_the_program]. If not, see <http://www.gnu.org/licenses/>.
  • Everything should be declared inside namespace (e.g. poplib for the populous library).
  • Class should have separate declaration file with class name as a filename. For libraries declaration should be placed in the include/ directory and implementation in the src/. Use *.h extension for the declaration and *.cpp for implementation.
  • Code should be portable apart from platform specific functionality which should be wrapped in macro.
  • Use 4 spaces for indent and store source files using UTF8 encoding.

[edit] C++ standards

  • Always prefer the Qt/STL over custom containers / algorithms.
  • Always prefer C++ techniques over C - Avoid C-strings (char* and functions like sprintf, strcpy); Avoid old I/O routines (fopen); Avoid malloc and use new for memory allocation; Use const for constant variables, not #define.
  • Always declare destructors 'virtual' unless the class you are writing should not have any vtable (no other virtual methods).
  • Use type casting from C++ instead of C (eg. static_cast(5.0) instead of (int) 5.0)
  • To handle fatal errors use exceptions.
  • While dealing with methods which can throw exception and using dynamic memory (new) remember to use smart pointers (auto_ptr).

[edit] Naming and documentation

  • Classes, types and structures should be title case (MyNewClass).
  • Methods and local variables should be camel case (myNewMethod).
  • Member variables should be prefixed with 'm' (mInstanceVar).
  • Preprocessor macros should be all upper case.
  • Enums should be named in title case, enum values should be all upper case.
  • All classes and methods must be fully documented in English using Doxygen-compatible comments. Use the @param and @returns directives to define inputs and outputs clearly, and @note to indicate points of interest.
  • Use verbose, descriptive names for classes, methods, variables - everything except trival counters. Code should be self-describing, don't be obtuse.

[edit] Style issues

  • Insert a newline before an open brace.
  • Use typedefs to declare template-based types that you use to avoid ugliness e.g. typedef std::list MyTypeList.
  • Always insert spaces in between operators and operands (x + y, not x+y).
  • For any doubts feel free to ask on the forums. Forum can highlight code, but it has to be wrapped in the tags: [code]
Personal tools