syque.com

The Psychology of Quality and More

| Menu | Books | Share | Search | Settings |

C Style: Standards and Guidelines (contents)

CHAPTER 6 : Layout

PART 3 : LAYOUT

CHAPTER 6 : Code Layout
6.1 Basic principles of code layout
6.2 Use of Spaces
6.3 Use of blank lines
6.4 Use vertical alignment
6.5 Indentation level
6.6 Line wrapping
6.7 Braces
6.8 Use of parentheses
6.9 Nested single statement
6.10 Empty statements
6.11 'else..if'
6.12 'switch' statements
6.13 'do..while'
6.14 Labels
6.15 Data declarations
6.16 Function declaration
6.17 Preprocessor commands
6.18 Summary

<--Prev page | Next page -->

 

6.5  Indentation level

Indentation is another favorite topic for debate amongst programmers, with a wide range of preferences for the number of columns to leave between each nested level.

The primary purpose of indenting is that the indented line is clearly visible from lines at other indent positions. Common indentation is anywhere between 2 and 8 characters per level.

 

Indent level = 2

 

for ( EmpNo = 0; EmpNo < EMP_NO_MAX; EmpNo++ )
{
  if ( strcmp(Employee[EmpNo].Name, SearchName) == 0 )
  {
    CalcEmpSalary( &Employee[EmpNo] );
    CalcTotal++;
    for ( i = 0; i < EMP_STS_TOT; i++ )
      Employee[EmpNo].Status[i] = EMP_STS_NORMAL;
  }
}

 

An indent level of 2 helps the writer who is using only spaces, and not tabs, for indenting. It also allows more space on the line for statements, especially helping the use of long identifier names.

The problem with a small indent size is that the lines are so close (column-wise) that it can be easy to confuse the indentation level (especially in a larger example than this).

 

Indent level = 4

 

for ( EmpNo = 0; EmpNo < EMP_NO_MAX; EmpNo++ )
{
    if ( strcmp(Employee[EmpNo].Name, SearchName) == 0 )
    {
        CalcEmpSalary( &Employee[EmpNo] );
        CalcTotal++;
        for ( i = 0; i < EMP_STS_TOT; i++ )
            Employee[EmpNo].Status[i] = EMP_STS_NORMAL;
    }
}

-------------------------------------------------------------------

An indent level of 4 makes successive indent levels much clearer, whilst still allowing reasonable space for longer symbol names. However, if the default editor tabstop is 8 characters, and will not or cannot be reset, indenting becomes a clumsy combination of tabs and spaces. 4 characters is probably the most common indent level in use.

 

Indent level = 8

 

for ( EmpNo = 0; EmpNo < EMP_NO_MAX; EmpNo++ )
{
        if ( strcmp(Employee[EmpNo].Name, SearchName) == 0 )
        {
                CalcEmpSalary( &Employee[EmpNo] );
                CalcTotal++;
                for ( i = 0; i < EMP_STS_TOT; i++ )
                        Employee[EmpNo].Status[i] = EMP_STS_NORMAL;
        }
}

 

Indenting by 8 characters is convenient when this is the default tab position. The difference between indent levels is abundantly clear. However each additional nesting level does cause rapid tramping across towards the right hand screen boundary, particularly if long variable names are used. Nevertheless, this can be a blessing in disguise as it prompts a minimizing of deep nesting (which is a cause of additional complexity and consequent unreadability of the code).

 

<--Prev page | Next page -->

 

Site Menu

| Home | Top | Settings |

Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive |

And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact |

Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate |

 

You can buy books here

More Kindle books:

And the big
paperback book


Look inside

 

Please help and share:

 

| Home | Top | Menu |

© Changing Works 2002-
Massive Content -- Maximum Speed