At the beginning of each file (both header and implementation files) describe
what the file contains, what the purpose of the class (or other code) is, and
what assumptions are made (if any). Give the author name and a brief
description of how the code is used.
Function/method comments
-- For each function and method, include a brief description of the purpose.
Include pre-conditions and post-conditions (including input and output) when
it helps to clarify the function. This should be done both at the declaration
and the definition.
-- You must use a line of dashes to
separate functions so they can be easily found by the reader.
-- Use additional comments to describe each functional block
within a function or method, but typically there are not comments on every
line of code.
Public vs. private
-- Methods should be made public only when necessary (that is, they are part of
the interface with other modules).
-- Data members should almost never be made public.
-- In most cases, do not circumvent information hiding by using
operations that return internal, implementation-dependent data (e.g., pointers
or references to data members).
Function/method purpose
Each function or method should perform a single well-defined operation.
If your function or method accomplishes two tasks, break it in two functions.
This improves the modularity of the code.
Global variables and constants
-- Do not use global variables anywhere in your code. While these are necessary
in rare cases, you will not need to use them for any programs in this class.
-- Global constants are acceptable. Global constants are defined in the .h file
of the class they go with.
-- Very few numbers appear in the code body. Use constant identifiers!
Consts
-- Use const member functions when possible.
-- When passing by reference use const when possible.