Long Lines and Function Length ..................................................................
Keep lines to the width displayable on a single screen. Code that is off to the right is eas-
ily 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 (instead of
at the beginning of the following line) so that it is clear that the line does not stand alone
and that more is coming.
In C++, functions tend to be much 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.
Structuring switchStatements ......................................................................
Indent switches as follows to conserve horizontal space:
switch(variable)
{
case ValueOne:
ActionOne();
break;
case ValueTwo:
ActionTwo();
break;
default:
assert(“bad Action”);
break;
}
As you can see, the casestatements are slightly indented and lined up. In addition, the
statements within each case are lined up. With this layout, it is generally easy to find a
case statement and easy to then follow its code.
Program Text ..................................................................................................
You can use several tips to create code that is easy to read. Code that is easy to read is
generally easier to maintain.
780 Day 21
if (condition==true) {
j = k;
SomeFunction();
}
This format is considered harder to read because the braces don’t line up.