The Psychology of Quality and More |
CHAPTER 6 : Layout
6.8 Use of parenthesesThe 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 operationsIt 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 & */
ComparisonsComparisons in expressions noticeably benefit from parenthesizing (including in conditional expressions):
( (PoolLen < PI_MIN_LENGTH) || (PoolWidth < PI_MIN_WIDTH) )
ExpressionsEven 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 parenthesesThe 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))
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).
|
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 |