Good Programming Practices

Write your C++ programs in a simple and straightforward manner. This is sometimes referred to as KISS ("keep it simple, stupid"). Do not "stretch" the language by tying bizarre usages.

Read the manuals for the version of C++ you are using. Refer to these manuals frequently to be sure you are aware of the rich collection of C++ features and that you are using these features correctly.

Your computer and compiler are good teachers. If after carefully reading your C++ language manual you are not sure how a feature of C++ works, experiment using a  small "test program" and see what happens. Set your compiler options for "maximum warnings." Study each message you get when you compile your programs and correct the programs to eliminate the messages.

Every program should begin with a comment describing the purpose of the program.

Many programmers make the last character printed by a function a new line (\n). This ensures that the function will leave the screen cursor positioned at the beginning of a new line. Conventions of this nature encourage software reusability--a key goal in software development environments.

Indent the entire body of each function one level or indentation within the braces that define the body of the function. This makes the functional structure of a program stand out and helps make programs easier to read.

Set a convention for the size of indent you prefer then uniformly apply that convention. The tab key may be used to create indents, but tab stops may vary. We recommend using either 1/4-inch tab stops or (preferably) three spaces to form a level of indent.

Some programmers prefer to declare each variable on a separate line. This format allows for easy insertion of a descriptive comment next to each declaration.

Place a space after each comma (,) to make programs more readable.

Choosing meaningful variable names helps a program to be "self-documenting," i.e., it becomes easier to understand the program simply by reading it rather than having to read manuals or use excessive comments.

Avoid identifiers that begin with underscores and double underscores because a C++ compiler may use names like that for its own purposes internally. This will prevent names you choose from being confused with names the compiler chooses.

Always place a blank line before a declaration that appears between executable statements. This makes the declarations stand out in the program and contributes to program clarity.

If you prefer to place declarations at the beginning of a function, separate those declarations from the executable statements in that function with one blank line to highlight where the declarations end and the executable statements begin.

Place spaces on either side of a binary operator. This makes the operator stand out and makes the program more readable.

As in algebra, it is acceptable to place unnecessary parentheses in an expression to make the expression clearer. These parentheses are called redundant parentheses. Redundant parentheses are commonly used to group subexpressions in a large expression to make that expression clearer.

Indent the statement in the body of an if structure to make the body of the structure stand out and to enhance program readability.

There should be no more than one statement per line in a program.

A lengthy statement may be spread over several lines. If a single statement must be split across lines, choose breaking points that make sense such as after a comma in a comma-separated list, or after an operator in a lengthy expression. If a statement is split across two or more lines, indent all subsequent lines.

Refer to the operator precedence chart when writing expressions containing many operators. Confirm that the operators in the expression are performed in the order you expect. If you are uncertain about the order of evaluation in a complex expression, use parentheses to force the order, exactly as you would do in algebraic expression. Be sure to observe that some operators such as assignment (=) associate right to left rather than left to right.