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 areturn
for all code paths
main()
has a return type of int
return
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::cin
std::cout
std::cerr
and std::clog
std::cerr
is unbuffered while std::clog
is bufferedEnd 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
Prefer
int
anddouble
int
is the type of a literal integer, e.g., 123
int
as the standard C++ integer type. Only use long
e.g, 123l
, unsigned int
e.g., 123u
, and unsigned long long
e.g., 123ull
if specifically needed.size_t
and std::size_type
double
is the type of a literal floating-point value, e.g., 1.23
double
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
auto
when 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
for
over 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
const
VariablesUse the
const
specifier on a type whenever possible
Prefer
std::istream_iterator
over an input loop
const
Create 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_
Lakos#endif
is not necessary, and is typically only used for include files with a very complex preprocessor logicFilename | 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 allinline
specifier is banned in this class