Skip to content

Latest commit

 

History

History
128 lines (89 loc) · 1.64 KB

File metadata and controls

128 lines (89 loc) · 1.64 KB

C++

A mostly reasonable approach to C++.

  1. Basic Rules
  2. Formatting
  3. Naming

Basic Rules

Prefer auto keyword

Use auto to avoid redundant reputation of type names and simplify generic code.

DO:

auto age = GetAge();

DON'T:

int age = GetAge();

Formatting

Prefer spaces and indent 2 spaces at a time

Spaces are preferred to allow consistent alignment across different environments.

Prefer to align related code chunks with the least amount of indents

DO:

Foo device;
device.id        = 1;
device.instance  = CreateInstance();
device.style     = FOO | BAR | BAZ;

DON'T:

Foo device;
device.id = 1;
device.instance = CreateInstance();
device.style = FOO | BAR | BAZ;

Naming

Casing

File - Pascal case

MyFile.cpp

Class - Pascal case

class MyClass {...}

Method - Pascal case

class Foo
{
public:
  void MyMethod();
}

Property - Pascal case

class Foo
{
public:
  int MyPublicProperty;
}

Field - Lower snake case with trailing underscore

class Foo
{
private:
  int my_private_field_;
}

Parameter - Lower snake case

void Foo::Bar(int my_parameter) {...}

Variable - Lower snake case

void Foo::Bar()
{
  auto my_variable = 1337;
}

Avoid Hungarian Notation

The compiler and IDE can provide type information, prefixing this information is redundant and gets in the way of reading the code.

DO:

std::string username = "batman";

DON'T:

std::string strUsername = "batman";