Skip to content

Coding Standard

Ivar Jönsson edited this page Sep 20, 2023 · 2 revisions

Common Rules

  • Curly brackets should not be on the same line as other text (except for short single-liners in header files, such as getters/setters)
  • Opening and closing brackets should be in the same column

Spaces in expressions

  • No space between a function's name and parentheses '('
  • No space before a comma, semicolon, and parentheses
  • Single space before and after binary arithmetic operators
  • Single space before and after '?' and ':' in the conditional operator
  • No spaces around < and > in template argument lists
  • Single space between statements (if, while, for, switch, etc.) and opening '('

Loops & Ifs

  • Should always use curly braces unless it improves readability (when having multiple similar-looking statements after each other)
  • else should always be on it's own line

Namespaces

  • Should be in PascalCase
  • Should always have its own column

Classes

  • Should be in PascalCase

Structs

  • Should be in PascalCase

Type name aliasing (typedef, using)

  • Should be in PascalCase

Primitive Types

  • Don't use non-size explicit types (uint, int, short, long, etc)

Allowed types:

  • uint8_t
  • int8_t
  • uint16_t
  • int16_t
  • uint32_t
  • int32_t
  • uint64_t
  • int64_t
  • size_t
  • float
  • double

Enums

  • Should never use C-style enum, should always use enum class
  • Should be PascalCase
  • Every enumerate name should be PascalCase and descriptive

Example:

enum class SomeEnum
{
	On,
	Off,
	Invalid
};

Function Name

  • Should be PascalCase
  • Should start with a verb
  • Should be descriptive of what the function does

Variables

Non-member variables

  • Should be camelCase starting with a lowercase character

Example:

void Function()
{
	float functionLocalFloat = 0.f;
        //...
}

Member variables

  • Private/protected non-static member variables should have the prefix m_ and then a camelCase name
  • Public non-static member variables should have no prefix (such as in structs)
  • Static member variables should have the prefix s_ and then a camelCase name

Example:

class SomeClass
{
private:
	float m_floatVariable = 0.f;
	uint32_t m_uintVariable = 0;

	int32_t s_staticVariable = 0;
};

struct SomeStruct
{
	float floatVariable = 0.f;
	uint32_t uintVariable = 0;
};

Other variables

  • Global variables should have the prefix g_ followed by camelCase
  • Static variables should have the prefix s_ followed by camelCase
  • Macro names and compile-time constants should be all uppercase separated by an '_'

Example:

int g_globalVariable;
static int s_staticVariable;