If statements

Control statements like if , else if, for, while, switch, and else statements always have curly brackets on separate lines. Even if they are one line statements.

C++17 adds if initalizers. Similar to for loops, there should be a space in between the initializer and condition.

if(int foo = 5; foo != 4) {
    
}

REASONING: There is a very important reason for this seemingly petty rule. It clarifies a lot of confusion.

For example, this code is very confusing at first glance:

if(condition) foo(); bar(); 

With this style guide, the above code will become much clearer:

if(condition) {
  foo();
}
bar();

Another example of why this rule is absolutely important is because of what happend to OSX developers. They had some code which checked the signature of an SSL certificate. For those who don't know what that is, the function checked if an encryption was valid. Here was the code (in C:)

hashOut.data = hashes + SSL_MD5_DIGEST_LEN;
hashOut.length = SSL_SHA1_DIGEST_LEN;
if ((err = SSLFreeBuffer(&hashCtx)) != 0)
    goto fail;
if ((err = ReadyHash(&SSLHashSHA1, &hashCtx)) != 0)
    goto fail;
if ((err = SSLHashSHA1.update(&hashCtx, &clientRandom)) != 0)
    goto fail;
if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
    goto fail;
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    goto fail;
    goto fail;  
if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
    goto fail;

err = sslRawVerify(...);

Notice that the second goto fail always ran. This was a typo by OSX developers that actually opened up a HUGE security vulnerability. By always putting curly brackets, you prevent very hard-to-find errors such as this.

Logical Operators

When using logical operators such as && and || always put a space in between the operands. Single operand operators such as !, -, or ~ don't have a space.

See more details in the operators section.

Also, the same rules specified on the Line Length page apply to if statements.

if(condition1 && !condition2 || (condition3() && ~condition4)) {
    
}

Else

If you have an else-if the closing bracket and the else goes on the same line. Spaces go after the brackets.

BAD:

}
else{

}else
if(){

}
else
if()
{

GOOD:

} else if() {

} else{

Last updated