Coding Conventions

From C4 Engine Wiki

Jump to: navigation, search

This page lists the coding conventions that are used in the C4 Engine. This is merely a reference for those who are interested.

Formatting

  • Tabs are always used instead of spaces to indent. Tabs are 4 spaces wide.
  • Continued lines are indented at least one tab more than the first line in the statement being continued. Additional tabs are sometimes used where it helps readability.
  • Braces appear on their own lines, and the closing brace is always indented to the same column as its matching opening brace. There are two exceptions: 1) The while statement at the end of a do...while loop stays on the same line as the closing brace. 2) It's acceptable for an empty block {} to appear on the same line as the preceding statement. For example,
for (a = 0; a < 5; a++)
{
    // body
}

do
{
    // body
} while (condition);

while (Func()) {}
  • If one part of an if...else statement is in braces, then both parts are in braces. For example,
if (x) Func1();
else Func2();

if (y)
{
    Func1();
}
else
{
    Func2();
    Func3();
}
  • Return values and boolean expressions are always enclosed in parentheses for readability. For example,
bool b = (var > 10);

long x = (var == 5) ? 1 : 2;
return (x);
  • Every binary operator always has a space before it and a space after it.
  • There is never a space following a unary operator.
  • There is almost always a space following a comma. The one exception is when the () operator is used to address a matrix entry, in which case no space appears after the comma.
  • In a pointer declaration, there is always a space before the * character, and no space after. In a reference declaration, there is no space before the & character, and always a space after. For example,
Point3D *p;          // Pointer to Point3D
const Point3D& p;    // Reference to const Point3D
Point3D *& p;        // Reference to pointer to Point3D
  • When an initializer list is used to construct base classes or member variables, different formatting is used depending on whether there is more than one initializer. If there is a single initializer, then it is placed on the same line as the name of the class constructor and the colon. For example,
MyClass::MyClass(int x) : BaseClass(x)
{
    // constructor body
}
  • When there are multiple initializers in an initializer list, each one is placed on a separate line following the name of the class constructor on a two-tab indent. The colon stays on the first line. For example,
MyClass::MyClass(int x) :
        BaseClass(x),
        memberObject(1)
{
    // constructor body
}
  • If a member variable has a trivial constructor, then the member variable is initialized inside the body of the class constructor, and not in the initializer list. (This is easier to read, and the trivial constructor generates no code, so no inefficiency is introduced.) Otherwise, if a member variable has a constructor that actually does something, then it is always initialized in the initializer list.


Naming

  • All class types, enumeration types, typedefs, namespaces, and function names begin with an uppercase letter, and mixed case (a.k.a. camel case) is used thereafter. For example,
class SpotLight
enum RenderType
void MoveObject(void)
  • All variable and parameter names begin with a lowercase letter, and mixed case is used thereafter (with one exception listed next). For example,
particleCount
boxSize
  • The globally-accessible pointers to the manager singletons all begin with The, with an uppercase T. For example,
TheGraphicsMgr
TheSoundMgr
  • Constant values and enum values begin with a lowercase k and use mixed case thereafter. The first word following the k is generally something that describes the context in which the enum is typically used. For example,
kLightStatic
kRenderDepthTest
  • The file C4Constants.h defines several floating-point constants and places them in the C4::K namespace. Values in this namespace begin with a lowercase letter and do not have the k prefix. For example,
K::pi
K::sqrt_2
Personal tools