EECS 230: C++ Style Manual
-
Names
- Member variable names are appropriately descriptive of what
they stand for; names shouldn’t just reiterate the type.
- Variable and function names start with a lowercase letter.
- Class and struct names start with an uppercase letter.
- Type alias names may be initial uppercase, or initial lowercase
ending in
_t
.
-
Variables
- Variables are initialized when defined if possible.
- Variables are defined in the narrowest possible scope.
-
Formatting
- Lines are no longer than 80 characters.
- Indentation is consistent and properly reflects code
structure.
- Indentation is by two or preferably four spaces; tabs
are unacceptable because they do not display the same
everywhere.
- Long blocks of code are separated into “paragraphs”
using blank lines.
- Infix operators generally have space on both sides.
- Commas and semicolons have a space after but no space
before.
-
Comments
- Each top-level definition (function, struct, class) has
a header comment succinctly stating its purpose.
- Excessive comments that inhibit readability are
avoided. Assume your reader understands the language; only
explain the non-obvious.
-
Class Design
- Constructors ensure that every object is properly
initialized.
- Every member is protected by the strictest level of privacy
that will work for it.
- Getters and setters are only provided where necessary,
and not where they break abstraction.