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.9  Nested single statement

Control statements (if, for, while, do) may be followed by a single non-compound statement, which does not use braces:

 

if ( Cursor.X > MAX_CURS_X ) NewLine( Cursor );

 

Putting the following statement on the same line as the if is breaking the 'one action per line' principle. It is more normal to indent it on the following line:

 

if ( Cursor.XPosn > MAX_CURS_X )
    NewLine( Cursor );

 

This can cause problems if a line is inserted between these two lines and the braces that are now needed are forgotten. It can also be less clear if the comparison wraps to the next line, or there is multiple nesting:

 

if ( (Cursor.XPosn > MAX_CURS_X)
    || (ParaSize > MAX_PARA_SIZE) )
    while ( !IsValid(LineNo++) )
          NewLine( Cursor );

 

This can be made clearer, and the potential for error reduced by using braces.

 

if ( (Cursor.XPosn > MAX_CURS_X)
    || (ParaSize > MAX_PARA_SIZE )
{
    while ( !IsValid(LineNo++) )
    {
        NewLine( Cursor );
    }
}

 

This reduces the chance of error and makes later line insertions easier. It also simplifies the decision as to whether to use braces or not. This is, however, at the cost of one or two additional lines per simple statement.

A fair compromise is to allow braces to be dropped in defined simple situations, where the comparison remains on one line and the next line is a simple expression, also one line:

 

if ( (Cursor.X > MAX_CURS_X)            /* non-simple case..    */
    || (ParaSize > MAX_PARA_SIZE )
{                                       /* ..so use braces      */
    while ( !IsValid(LineNo++) )        /* simple case..        */
        NewLine( Cursor );              /* ..so no braces used  */
}

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

Note that braces should not be dropped if the following 'statement' is a macro, as the resulting expansion could cause unexpected results.

 

<--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