Personally I consider indentation style not so important, since
• filters can be used to adjust source code62 (e.g. indent)
• syntax coloring editors can be used to adjust source code (e.g. xemacs)
• a programmer can often easily adapt to a style
However, independently of the indent style you use,
• be consequent
• try not to make assumptions about other editors tabulator settings
Tabulators
Making no assumptions about tabulator settings restricts you to either use only tabulators to indent or only blanks, but not both (since the results are displayed differently63). Tabulator setting independence forbids also the usage of tabulators at other places than at the left margin.
Not all editors can preserve tabs or blanks64. In a worse case, only the indentation of changed lines in a source file is converted.
Tabulators can also become victims of branch merge tools (which are part of revision control software).
Braces
Opening and closing braces ({}) can either appear on a line of their own or on the preceding line. The closing braces being right after the last statement (Lisp style) being rarely seen.
If the opening and/or closing braces are on a line of their own, they can be adjusted to the indent level of the outer block or to that of the inner block or (halfway) in between.
The use of the above styles can differ between code (functions) and data (structs, unions, array initializations) and can differ between top level code braces (functions) and function level code braces (do, else, for, if, switch, while).
Braces may or may not be omitted in control blocks if the block covers one or zero statements65.
Labels
Switch labels (case, default) can either appear adjusted to the outer block indent level or to the inner (with or without adding one more indent level for the code in the switch statement66) or in between.
Goto labels can be adjusted to the left margin (i.e. top level block), one indent level less than the next statement, or on the same level as the next statement. The first two styles are more readable.
Be consequent about placing the labels.
Blanks
Only few C tokens (identifiers, operators, etc.) require a blank as delimiter between them (e.g. 'else if' and 'int i'). However, lots of blanks are typically used to make source code more readable.
Be consequent about using blanks between tokens.
Trailing blanks and tabulators change only the meaning of trailing backslashes (e.g. in C++ style singleline comments67 where they're legal and have a semantic). Using trailing blanks and tabulators in this context is dangerous, since editors may be inaccurate in preserving them.
Friday, January 9, 2009
Indentation
Posted by abhilash at 9:00 AM
Subscribe to:
Post Comments (Atom)
0 Comments:
Post a Comment