The Psychology of Quality and More |
CHAPTER 6 : Layout
6.2 Use of SpacesSpaces, for the most part, are not really necessary in C, and it is possible to write code without using them:
if(Wgt.Hgt>((StdHgt+WGT_OFFSET)*WGT_CRCTN)) ------------------------------------------------------------------------ The main use of spaces is to increase the readability of the code. Thus we could put a space between all tokens in the above code:
if ( Wgt . Hgt > ( ( StdHgt + WGT_OFFSET ) * WGT_CRCTN ) )
This makes the individual tokens clearly identifiable, but some items are now too dissociated. A balance must be struck between using space for readability and not spacing items to maintain a close association. There is also a horizontal cost of using spaces within a line which can cause the line to wrap and possibly become less readable. A simple set of rules which may be used for spacing tokens is:
Individual instances, which may include exceptions to the above rules, are discussed below. 6.2.1 Spacing object referencesA structure or array reference, although made up of identifiers and tokens, is a closely associated reference to a single object. So do not space structure and array references : ., ->, []:
Para.Length, Window->Size, EmpName[10]
6.2.2 Spacing unary operatorsUnary operators are closely associated with one token, thus do not put a space between unary operators and their operand: !, ~, ++, --, -, * (pointer indirection), &, (casts), sizeof:
!DoorIsOpen, ~PortEnables, WgtNo++, --WgtNo, -10,
Do put a space on the side away from the operand, as this helps to make the association with its token clear. It also avoids 'gotchas', for example where '/*' is taken as the start of a comment:
*Current = *Volts/*Resistance; /* *Current = *Volts ? */
6.2.3 Spacing binary operatorsBinary operators are quite closely associated with their operands, but they can be seen more clearly when they are spaced either side - particularly when there are long identifier names and other operators about. These operators include: +, -, *, /, %, >>, <<, <, <=, =>, >, ==, !=, &, |, &&, ||, ? :, =, +=, etc.
WordNo = FirstWordNo + *CurrWordOffset;
It might be permissible to not use spaces in situations to emphasize precedence, although this is complicating a simple rule, and there is a danger of incorrect usage causing misunderstanding:
if ( WdStart+WdOffset > MAX_OFFSET*WdSize ) /* this is ok */
..but..
if ( WdStart+WdOffset / WdSize > MAX_OFFSET ) /* this is not! */
6.2.4 Spacing comma and semicolonThe comma, ',', and the semicolon, ';', are more closely associated (albeit loosely) with the previous item than the following item. Thus, they may have no space before and a space after. Note that this is the same usage as in the written form of the English language:
for ( i = 0, j = 0; i < WGT_MAX; i++, j++ )
Applying all of the above rules to the original example in 6.2 now gives:
if ( Widget.Hgt > ((StdHgt + WGT_OFFSET) * WGT_CORRN) )
6.2.5 Spacing parenthesesIn all parenthesized items, the contents can be made to stand out more by putting a space after the '(' and before the ')'. In nested parentheses, the inner sets of parentheses may not be spaced, to differentiate them from the outer set of parentheses. In any case by always matching the spacing on any pair of parentheses, matching pairs are made easier to identify:
if ( CurrPage > (FirstPage - LastPage) )
..not..
if ( CurrPage > (FirstPage - LastPage ))
It is similarly useful to try to match the spacing on the outside of parentheses, although this is not always possible with nested sets of parentheses:
(FirstPage + (CurrPage - LastPage))
The first parenthesis of a function call is closely associated with the function name, helping to identify it as a function. Also, if it was actually a macro, the parenthesis would have to match the macro. Thus, do not put a space between a function name and its first parenthesis:
CalcWindowSize( Window );
6.2.6 Spacing keywordsKeywords are not functions, and may be shown to be such by spacing them out:
if ( DiscIsFull )
Possible exceptions to this are return and sizeof:
'return'The return keyword is followed by an optional expression:
return LineStatus || COLLISION_MASK;
It is common to use parentheses to more clearly delimit the return'ed expression, even when returning a single value. return is often viewed (erroneously, but harmlessly) as being a function, which would suggest that there be no space after it. The reader may be reminded of its keyword status by retaining the following space:
return ( LineStatus || COLLISION_MASK );
'sizeof'The sizeof keyword has different rules for objects and types. Objects do not need parenthesizing, but types do:
sizeof Para ..but.. sizeof (int)
This subtlety can cause confusion and error. The simplest approach is to use parentheses on both. In any case, sizeof is a unary operator, and by the arguments above (see 6.2.2) a space should not be put between an unary operators and its operand. To do this, sizeof objects needs to use parentheses as separators:
sizeof(Para) ..and.. sizeof(int)
|
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 |