git commit. The commit message will describe the purpose of the code changeEvery program file needs a header comment
main()) or what the file is forInclude files should be at the top of the file before any other statements
return In main()
main()requires areturnfor all code paths
main() has a return type of intreturn leads to an undefined return value when the program runs0 indicates that the program was successfulusing namespace std;main() functionWrite error and log messages to standard error
std::cout is for the standard output streamstd::cerr is for the standard error stream| C++ Stream | Standard Stream | Standard File Descriptors | Buffered |
|---|---|---|---|
| std::cin | standard input | stdin | Yes |
| std::cout | standard output | stdout | Yes |
| std::cerr | standard error | stderr | No |
| std::clog | standard error | stderr | Yes |
End final output of the program with a newline
Separate each section of statements with a single, blank line
Every section of code needs a comment describing the purpose
Declare only one variable in a declaration statement
intPrefer
intfor the integer variable type
int is the type of a literal integer, e.g., 123int as the standard C++ integer number type. Only use long, e.g., 123l, unsigned int, e.g., 123u, and unsigned long long, e.g., 123ull, etc., if specifically needed.size_t and std::size_typedoublePrefer
doublefor the floating-point variable type
double is the type of a literal floating-point value, e.g., 1.23double is the type for all of the standard math functions, e.g., log()double as the standard C++ floating-point number type. Only use float if specifically needed, e.g., SSE instructionsInitialize local variables where they are declared
All variable names should be descriptive of what they store
auto KeywordUse
autowhen initializing a declaration that includes a non-literal expression
auto when initializing with a literal value to make certain you get the intended type
int total = 0;double x = 0;Put the calculations of each result in a separate section
Use only simple expressions in output statements
Prefer range-based
forover indexing
The for statement with indexing can lead to an infinite loop
Range-based for is safer and does not lead to infinite loops with standard containers
Extension of using iterators
for Statementconst VariablesUse the
constspecifier on a type whenever possible
Prefer
std::istream_iteratorover an input loop
constCreate new functions for sections of code with loops and multiple calculations
// output the rainfall report because that code belongs to main()'.' with an underscore '_'INCLUDED_ (see [Lakos])| Filename | Include Guard Name |
|---|---|
| XMLReader.hpp | |
| xmlReader.hpp | |
| xml_reader.hpp |
When performing a standard operation on a container, look for existing solutions
If a function is not doing very much, consider replacing the call with the calculation
inline specifier, which often has no effect at all