Place opening brackets on the same line as the statement.
if (cool == true) {
// ok!.
}
if (cool == true)
{
// Not ok!.
}
Leave space before and after the parenthesis for if/while/for/catch/switch/do
// consistent.
if (something) {
}
// inconsistent.
if(something){
}
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 {
};
When executing a function, leave no space between it's name and parens.
// consistent.
eatIceCream();
// inconsistent.
eatIceCream ();
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;
}
If a block contains only a single statement it must be placed in a separated line without brackets
if (something)
call_superman();
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;
Under construction