|
|
|
|
|
|
|
Keep lines to the width displayable on a single screen. Code that is off to the right is easily overlooked, and scrolling horizontally is annoying. When a line is broken, indent the following lines. Try to break the line at a reasonable place, and try to leave the intervening operator at the end of the previous line (as opposed to the beginning of the following line) so it is clear that the line does not stand alone and that there is more coming. |
|
|
|
|
|
|
|
|
In C++, functions tend to be far shorter than they were in C, but the old, sound advice still applies. Try to keep your functions short enough to print the entire function on one page. |
|
|
|
|
|
|
|
|
Tab size should be four spaces. Make sure your editor converts each tab to four spaces. |
|
|
|
|
|
|
|
|
Indent switches as follows to conserve horizontal space: |
|
|
|
|
|
|
|
|
switch(variable)
{
case ValueOne:
ActionOne();
break;
case ValueTwo:
ActionTwo();
break;
default:
assert(bad Action);
break;
} |
|
|
|
|
|
|
|
|
There are several tips you can use to create code that is easy to read. Code that is easy to read is easy to maintain: |
|
|
|
|
|
|
|
|
Use white space to help readability. |
|
|
|
|
|
|
|
|
Objects and arrays are really referring to one thing. Don't use spaces within object references (., ->, []). |
|
|
|
|
|
|
|
|
Unary operators are associated with their operand, so don't put a space between them. Do put a space on the side away from the operand. Unary operators include !, ~, ++, -, -, * (for pointers), & (casts), and sizeof. |
|
|
|
|
|
|
|
|
Binary operators should have spaces on both sides: +, =, *, /, %, >>, <<, <, >, ==, !=, &, ¦, &&, ¦¦, ?:, =, +=, and so on. |
|
|
|
|
|
|
|
|
Don't use lack of spaces to indicate precedence (4+ 3*2). |
|
|
|
|
|