Coding Standard

Identifying all the components contributing to a well-designed program is challenging. The following list serves as a starting point and is used to evaluate class projects. While there may be some exceptions, they are not typical and should not overshadow the general guidelines. Consider how these guidelines apply when reviewing a program rather than focusing on potential exceptions. The checklist will be extended as the semester progresses.

Files and Directories

Only source files belong in the repository:

Generated files do not belong in the repository:

Generated files take a lot of filespace and prevent others from easily cloning the repository and building.

Overall Program

Indentation

Spacing

Code Explanation
Single space after start of comment
Single space after keywords of statements
Single space before operators See above
Single space after operators See above
No space between call name and argument list
No space before statement-end semi-colon

Naming

Statements

Functions

Parameter Types

Use the following tables for type T:

Direction Primitive Object
IN T const T&
OUT, IN/OUT T& T&

Examples:

Return Types

For free functions:

Type Application Example
T Return copy of a local variable
T Return copy of a parameter
const T& Return a read-only reference to a parameter

Comments

The following applies to all comments except file header comments.

std::string

Prefer the default and methods common to a container:

Prefer   Explanation
By default a std::string is an empty string
A std::string is a (specialized) container. Prefer container methods over comparison. One exception might be in a set of if statements (such as a nested if) that compares the string to other literal strings.
Prefer container method to assignment

std::string_view

The new string type std::string_view was an addition to C++ in C++17. It allows you to efficiently refer to parts of a pre-existing std::string or a c-string char* without making a copy. When used as a parameter with a c-string argument, e.g., "abc", it does not create a std::string. It has many of the same methods as the std::string.

std::string std::string_view
void f(const std::string& s); void f(std::string_view);

If an IN parameter is used, it is typically passed by value instead of const reference as it is small.

Debugging Output

Write debugging/tracing messages to std::cerr, not std::cout:

std::cerr for error output