Code Style Guide
This page describes the style used for Arx Libertatis source code. New contributions should try to follow this guide, even if much of the existing code doesn't. However, nothing described here are is a strict rule and
Example
(WIP!)
/* * Copyright notice(s) ... */ #ifndef ARX_SOMEDIR_SOMECLASS_H #define ARX_SOMEDIR_SOMECLASS_H namespace somens { //! Short documentation comment void someFunction(); class SomeClass { /*! * Longer documentation comment * * @param functionParameter Some parameter... * * @return some value. */int
memberFunction(int
functionParameter);int
m_memberVariable; staticint
s_staticMemberVariable; }; externint
g_globalVariable; } // namespace somens #endif // ARX_SOMEDIR_SOMECLASS_H
#include "somedir/SomeClass.h" namespace somens {int
g_globalVariable = 0;int
SomeClass::s_staticMemberVariable = 0;int
SomeClass::memberFunction(int
functionParameter) {int
localVariable = functionParameter; someFunction(); return localVariable + m_memberVariable; } } // namespace somens
Symbol Names
Symbol names use variants of Template:Wp.
Classes
Classes and structs use camel case names and start with an upper case letter:
struct SomeType;
TODO: what about typedefs?
Functions
Function names are also camel case, but start with a lowercase letter:
void someFunction();
Member functions
Member functions are also just functions - there is no need for a different visual style.
Variables
TODO: note on no hungarian notation
Local variables
Local (function-scope) variables use camel case names with a lower-case start:
int
localVariable = 0;
Function parameters
Function parameters are local variables, so they should follow the same naming scheme.
Member variables
Member function variables should be named just like local variables, but with and added m_
prefix:
int
m_someVariable;
TODO decide on prefix if any:
- Easy to distinguish local variables from those with a longer lifetime
Static member variables
Static function-scope variables
Global variables
... should be avoided if possible!
Namespaces
Namespace names should be lowercase and as short as possible.
If the namespace block contains more than a few lines, consider repeating the namespace keyword and name at the end:
namespace ns { // ... } // namespace ns
Low-level symbols
Low-level classes and their member functions and variables (container, path, ...) use C++-stdlib-style lowercase names with underscores. This is so that they visually fit in with other lower-level symbols from the C++ stdlib and Boost.
Formatting
Line length
Indentation
Empty lines
Alignment
Line breaks
Other whitespace
Documentation
Other notes
using namespace
statements
... should be avoided. Use namespace aliases instead:
namespace a = b::c::d;
However, try to use a meaningful alias. If possible, aliases consistent aliases should be used for the same source namespace.
other using
statements
... are also usually best avoided. There are of course exceptions (for example to invoke ADR with std::swap
), but leaving the namespace prefix where the symbol is used makes it easier to tell where the symbol comes from.