Good Practices

Many details make up a “good” program, with the characteristics that we discuss in the course. The following is just a start towards listing them. The items below are considered in the grading of a class project.

Creating a list of these is not easy, as there are always exceptions. However, these exceptions are not common. Think about where this would apply, not what the exception would be.

The checklist will be extended as the semester progresses.

Files and Directories

Indentation and Spacing

Overall Program

Naming

Statements

Functions

std::string

The std::string default constructor initializes to an empty string. So instead of:

just declare the string:

A std::string is a (specialized) container. Use container methods over comparison. So instead of:

just check that it is empty, just like other containers:

One exception might be if you are in a set of if statements (such as a nested if) and are comparing the string to other literal strings.

Also, if you need to change to value to an empty string, instead of:

just clear out the data, just like other containers:

Parameter Types

Direction Primitive Object
in T const T&
out, inout T& T&

Examples:

As always, some specific cases:

Return Types

Type Application
T Return copy of local variable or field
const T& Return read-only reference to field

Examples:

Classes

Comments

The following applies to all comments, except file header comments.

Debugging Output

Write debugging/tracing messages to std::cerr, not std::cout. First, the program already uses std::cout for output. Second, std::cerr is not buffered, while std::cout is. Why does this matter? If your program crashes, any unbuffered output may be lost. So you may not see your debugging messages.

Prefer:

to: