Note: I wrote these as I went. They are in no particular order.
- Braces should always be on their own line with one exception: When being used to declare a function in a class that has no code. Ex:
if(expression)
{
// Code.
}
class Derived : public Base
{
public:
// In this case, it prevents two unneeded lines. This is acceptable and more readable.
Derived() : Base(variable) {};
};All blocks and statements should include braces. Even if there is only one line of code executed after the statement is evaluated.
- Namespaces should always be lowercase:
namespace fslib
{
}- Classes and types: Upper camel case. Ex:
class ClassName
{
public:
ClassName() = default;
private:
// Member variables.
};
typedef struct
{
int data;
} StructName;
typedef enum
{
Type1,
Type2,
Type3
} EnumName;- Member functions and methods: Lower snake case. Ex:
class ClassName
{
public:
ClassName() = default;
void member_function_name();
};- Member methods should be called prefixed with the class they belong to to avoid confusion when inheritance and polymorphism is involved. Ex:
class BaseClass
{
public:
void member_method();
}
class DerivedClass : public BaseClass
{
public:
void member_method_b()
{
BaseClass::member_method();
}
}- Class member variables: Prefixed by either
m_for members, orsm_for static members followed by lower camel case for the variable name. Ex
class ClassName
{
public:
ClassName() = default;
private:
int m_classNameVariable = 0;
static inline int sm_staticSharedVariable = 0;
};- Functions: Lower snake case, prefixed with the namespace they belong to. No
namespace x{in source files. Ex:
void fslib::function_name()
{
// Do stuff here.
}- Function arguments: Lower camel case. Ex:
void fslib::function_name(int argName)- Local variables: Lower camel case. Ex:
int numberHere = 0;- Global variables should be avoided at all costs. If it can't be, they should be prefixed with
g_followed by lower camel case name. Ex:
int g_globalVariableName = 0;- Static and anonymous namespace variables should be prefixed by
s_followed by lower camel case. Ex:
static int s_variableName = 10;
namespace
{
constexpr int s_variableName = 10;
}- C style macros and all constants should be all capital with underscores. C style macros should be avoided and used sparingly unless they can drastically clean up code. Ex:
#define MACRO_NAME(x)
static constexpr int CONST_VALUE_NAME = 10;
namespace
{
constexpr int CONST_VALUE_NAME = 10;
}