The Psychology of Quality and More |
CHAPTER 6 : Layout
6.17 Preprocessor commandsPreprocessor commands are the poor relations in C. They are seldom viewed as 'real C' statements and have various restrictions on their layout. The result is that they are often treated as being almost invisible, and their layout is ignored. Preprocessor command layout is constrained in older compilers by the position of the '#', which must be in column 1. Sometimes also, the keyword must immediately follow the '#'. 6.17.1 Preprocessor comparisonsPreprocessor comparisons (#if, #ifdef) with everything left-justified are difficult to read, as the lack of indentations makes the nesting level unclear:
#ifdef DEBUG
Compilers often parse the '#' separately from the keyword, allowing indentation to be used for preprocessor comparisons:
#ifdef DEBUG
ANSI C removes all restrictions on the positioning of the '#' and its keyword. 6.17.2 ConstantsConstants are similar to data definitions, and can be laid out similarly, using vertical alignment:
#define PACKET_SIZE 20
‘enum’s are often declared on a single line, especially where their meaning needs no comment:
enum CAR_MAKER { FORD, JAGUAR, VOLKSWAGON };
If there are a lot of items, or if comments are needed, then the vertically aligned layout may be used:
enum CAR MAKER
6.17.3 MacrosMacros are similar to function definitions, and can be laid out similarly, using the principle of 'one action per line' with the continuation character tabbed out of the way of the code:
#define TRACE_IN(FnName) \ ------------------------------------------------------------- For simple macros which perform only one or two actions, this may considered to be too complex, and keeping it to a single line may be acceptable.
|
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 |