Skip to content

Latest commit

 

History

History
119 lines (80 loc) · 1.69 KB

SG-CPP.md

File metadata and controls

119 lines (80 loc) · 1.69 KB

C++ style guide

Brackets

Place opening brackets on the same line as the statement.

if (cool == true) {
  // ok!.
}

if (cool == true)
{
  // Not ok!.
}

Parenthesis

Leave space before and after the parenthesis for if/while/for/catch/switch/do

// consistent.
if (something) {

}

// inconsistent.
if(something){

}

Naming : variables, functions, classes, ...

As C++ standard, use lower case letters. If the name has many words, they must be separated by underscore

// consistent
int very_cool_name;

// inconsistent.
int BadExample:
// consistent.
class cool_name {

};

// inconsistent.
class ThisIsNotJava {


};

Function Spacing

When executing a function, leave no space between it's name and parens.

// consistent.
eatIceCream();

// inconsistent.
eatIceCream ();

Return Early

Return early wherever you can to reduce nesting and complexity.

int some_calculation(int x) {
  if (x < 10)
    return 0;

  int ans = 10;
  /*
    Some logic here
  */
  return ans;
}

One line statements

If a block contains only a single statement it must be placed in a separated line without brackets

if (something)
  call_superman();

Operators

Unary operators are not separated from the expression in any way:

counter++;

Binary operators should be separated from adjacent expression by spaces:

int z = x + y;

Other stuff

Under construction

Cool references