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.8  Use of parentheses

The operators' precedence table is quite complex, separating 45 operators into 15 levels of precedence. Parentheses have the highest priority - this is necessary for changing the precedence of operators lower down the table. Parentheses can also be used when they are not necessary, to emphasize chunks and to remind the reader of the actual preference.

 

Bit operations

It is worth always using parentheses around bit operations as they probably have the most non-intuitive precedence:

 

(SIOPort & SIO_BUSY) >> SIO1_BITS     /* >> is higher priority than & */

 

Comparisons

Comparisons in expressions noticeably benefit from parenthesizing (including in conditional expressions):

 

( (PoolLen < PI_MIN_LENGTH) || (PoolWidth < PI_MIN_WIDTH) )

(VoterAge > MIN_VOTE_AGE) ? CAN_VOTE : CANNOT_VOTE

 

Expressions

Even where the precedence is the same, parentheses can be used to emphasize the intent of the programmer:

 

WallArea - (DoorArea + WindowArea)   /* subtract holes from overall area */

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

6.8.1  Nesting parentheses

The simplest rule for when to parenthesize is 'everywhere', although this can result in a confusing level of parenthesis nesting:

 

( (Case1 + ((SysErr1 / (ERR_1_CORR + SYS_CORR)) + CaseX))
 - (Case2 * (SysErr2  / (ERR_2_CORR + SYS_CORR)) - CaseY) )

 

Deep levels of parenthesis nesting, particularly if the expression wraps to another line, can detract more than it gives. A guideline for a maximum level of nesting of parentheses can be given, typically around three (see 2.10).

 

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