Formatting 2
Conditionals
Prefer no spaces inside parentheses. The if and else keywords belong on separate lines.
There are two acceptable formats for a basic conditional statement. One includes spaces between the parentheses and the condition, and one does not.
The most common form is without spaces. Either is fine, but be consistent. If you are modifying a file, use the format that is already present. If you are writing new code, use the format that the other files in that directory or project use. If in doubt and you have no personal preference, do not add the spaces.
Note that in all cases you must have a space between the if and the open parenthesis. You must also have a space between the close parenthesis and the curly brace, if you’re using one.
If one part of an if-else statement uses curly braces, the other part must too:
Switch Statements
Switch statements may use braces for blocks. Annotate non-trivial fall-through between cases. Braces are optional for single-statement loops. Empty loop bodies should use either empty braces or continue.
case blocks in switch statements can have curly braces or not, depending on your preference. If you do include curly braces they should be placed as shown below.
If not conditional on an enumerated value, switch statements should always have a default case (in the case of an enumerated value, the compiler will warn you if any values are not handled). If the default case should never execute, treat this as an error. For example:
Loops
Braces are optional for single-statement loops.
Empty loop bodies should use either an empty pair of braces or continue with no braces, rather than a single semicolon.
Pointer and Reference Expressions
No spaces around period or arrow. Pointer operators do not have trailing spaces.
The following are examples of correctly-formatted pointer and reference expressions:
Note: There are no spaces around the period or arrow when accessing a member. Pointer operators have no space after the
*or&.
Return Values
Do not needlessly surround the return expression with parentheses.
Use parentheses in return expr; only where you would use them in x = expr;.
Variable and Array Initialization
Your choice of =, (), or {}.
You may choose between =, (), and {}; the following are all correct:
Be careful when using a braced initialization list {...} on a type with an std::initializer_list constructor. A nonempty braced-init-list prefers the std::initializer_list constructor whenever possible. Note that empty braces {} are special, and will call a default constructor if available. To force the non-std::initializer_list constructor, use parentheses instead of braces.
Also, the brace form prevents narrowing of integral types. This can prevent some types of programming errors.
Class Format
Sections in public, protected and private order, each indented one space.
The basic format for a class definition is:
Things to note:
- Any base class name should be on the same line as the subclass name, subject to the 80-column limit
- The
public:,protected:, andprivate:keywords should be indented one space. - Except for the first instance, these keywords should be preceded by a blank line. This rule is optional in small classes.
- Do not leave a blank line after these keywords.
- The
publicsection should be first, followed by the protected and finally the private section. - See Declaration Order for rules on ordering declarations within each of these sections.
Namespace Formatting
The contents of namespaces are not indented.
Namespaces do not add an extra level of indentation. For example, use:
Do not indent within a namespace:
When declaring nested namespaces, put each namespace on its own line.