Barr Group FacebookBarr Group TwitterBarr Group LinkedInBarr Group Vimeo

Rules

The following C coding rules relate to the indenting of lines:

Rule 3.4.a.) Each indentation level within a module should consist of 4 spaces.

Rule 3.4.b.) Within a switch statement, each case statement should be indented; the contents of the case block should be indented once more.

Rule 3.4.c.) Whenever a line of code is too long to fit within the maximum line width, indent the second and any subsequent lines in the most readable manner possible.

Example

sys_error_handler(int err)
{
    switch (err)
    {
        case ERR_THE_FIRST:
            ...
        break;
      
        default: 
            ...
        break;
    }

    // Purposefully misaligned indentation; see why?
    if ((first_very_long_comparison_here
         && second_very_long_comparison_here) 
        || third_very_long_comparison_here) 
    {
        ...
    }
}

Reasoning

Fewer indentation spaces (2 vs. 4) increase the risk of visual confusion while more spaces (8 vs. 4) increases the likelihood of line wraps.

Exceptions

The indentation in legacy code modules that are indented differently shall not be changed unless it is anticipated that a significant amount of the code will be modified. In that case, the indentation across the entire module shall be changed in a distinct version control step. This is because a side effect of changing indentation is the loss of difference tracking capability in the version control system. It is thus valuable to separate the code changes from the indent changes.

Enforcement

An automated tool shall be provided to programmers to convert indentations of other sizes automatically. This tool shall modify all new or changed code prior to each build.

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

Sign up for our newsletter today!

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.