Barr Group TwitterBarr Group Vimeo

Rules:

1.3.a. Braces shall always surround the blocks of code (a.k.a., compound statements), following if, else, switch, while, do, and for statements; single statements and empty statements following these keywords shall also always be surrounded by braces.

1.3.b. Each left brace ({) shall appear by itself on the line below the start of the block it opens. The corresponding right brace (}) shall appear by itself in the same position the appropriate number of lines later in the file.

Example:

{
    if (depth_in_ft > 10) dive_stage = DIVE_DEEP;	// This is legal... 
    else if (depth_in_ft > 0)
        dive_stage = DIVE_SHALLOW;	                // ... as is this.
    else
    {		                     // But using braces is always safer.
        dive_stage = DIVE_SURFACE;
    }
    ...
}

Reasoning: There is considerable risk associated with the presence of empty statements and single statements that are not surrounded by braces. Code constructs like this are often associated with bugs when nearby code is changed or commented out. This risk is entirely eliminated by the consistent use of braces. The placement of the left brace on the following line allows for easy visual checking for the corresponding right brace.

Enforcement: The presence of a left brace after each if, else, switch, while, do, and for shall be enforced by an automated tool at build time. The same tool or another (such as a code beautifier) shall be used to enforce the alignment of braces.

What’s happening and how it’s done. Get in the know.

Sign Up for Our Newsletter

Receive free how-to articles, industry news, and the latest info on Barr Group webinars and training courses via email. 

To prevent automated spam submissions leave this field empty.