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.18  Summary

  • Write code in a hierarchy of chunks.
  • Use white space to separate chunks - but don't overuse it.
  • Put one action per line.
  • Use Combs rather than Toothbrushes to save horizontal space and help vertical alignment.
  • Use spaces to create horizontal chunking.
  • No spaces in object references.
  • No space between unary operator and operand, but do space other side.
  • Balance spacing either side of binary operators.
  • No space before separators (comma, semicolon) but do space after.
  • Balance spacing inside parentheses.
  • No space between function name and parenthesis.
  • Space keywords.
  • Use blank lines to create vertical chunking.
  • Use vertical alignment to indicate association.
  • Choose an indentation level (eg. 4 spaces).
  • Indent continuation lines past the current indent column.
  • Decide on where to put continuation operator.
  • Use precedence rules to decide where to split an expression.
  • In a complex expression, treat parentheses as braces.
  • Wrap assignments past the equals sign.
  • Wrap conditional expressions before the operators.
  • Wrap for statements after the semicolons. Indent further if more wrapping occurs.
  • Wrap long function calls with one parameter per line.
  • Choose a brace style. Use in struct's etc. too.
  • Use parentheses to chunk horizontally and to emphasize priority.
  • Avoid deep (more than about 3) levels of parentheses nesting.
  • In macros, parenthesize everything.
  • Use braces with a nested single statement unless it, and the preceding line, are visibly and logically simple.
  • Show an empty statement on a single line (choose style).
  • Keep else..if vertically under original if.
  • Put case's directly under the switch and indent resultant code.
  • For long case's either use braces or break out the code into another function.
  • Put the while in a do..while statement on the same line as the closing brace.
  • Start labels in column 1.
  • Order declarations as: storage class specifier, type qualifier.
  • Start each declaration on a new line.
  • Use vertical alignment to ease scanning of declarations.
  • Don't combine declaration and definition of struct.
  • Define constants near associated declarations.
  • Define function declaration layout.
  • Consider putting the return type on the line before the function name.
  • Format preprocessor lines nicely too.
  • Lay out constants just like data
  • Split lines of complex macros using normal layout rules.

 

 

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-2015
Massive Content -- Maximum Speed