The Psychology of Quality and More |
CHAPTER 6 : Layout
6.9 Nested single statementControl 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 )
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)
This can be made clearer, and the potential for error reduced by using braces.
if ( (Cursor.XPosn > MAX_CURS_X)
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..
*/ ------------------------------------------------------------------- Note that braces should not be dropped if the following 'statement' is a macro, as the resulting expansion could cause unexpected results.
|
Site Menu |
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 |
And the big |