CSCI at CU: C++ Style Guide
Version 4.0 – January 5, 2005
In order to allow easy reading and understanding of everybody’s programs, and to avoid risky programming practices, all software organizations have guidelines that programmers must follow. This page contains the guidelines we require you to follow this semester. For your interest, you might also look at a real-world programming stylesheets.
At the same time, this page defines abbreviations that instructors can use in providing feedback on your programs. For example, if we mark II on a program you turn in, this sheet tells you that we think you have used incorrect indenting.
So, here’s a list of bad things we’ll be looking for in your programs, and the abbreviations we’ll use to notify you if we find them.
- Each .cxx or .h file must have a comment at the top indicating the file name, the name and email of the programmer (that’s you), the date it was written, and a concise but complete description of the purpose.
- There are many ways people indent their C++ code, but the one way that we’ll accept is described here. This way has some advantages in readability, but more than that (1) if you are all consistent it helps us read your code, and (2) in real life you’ll have to get used to following specific guidelines in your code, so why not get some practice now.
FCI File comment incomplete.
II Incorrect indenting.
The rules are:
- (a) opening curly braces are always placed on a line by themselves immediately below the first character of the keyword that controls the code in the brace, such as for, if, while or else, or the header of a function. This line may also contain a comment starting with //.
- (b) if you prefer, the entire body of a function may be indented an extra four spaces beyond the line that contains the function name and parameters.
- (c) lines inside braces are indented four spaces to the right of the brace
- (d) closing curly braces always occur on lines by themselves immediately below the corresponding opening brace
(e) With an if, else, for, while or similar construct that controls a single statement, without curly braces, indent the statement four spaces.
For example…
void foo(int x, int y) { int i; for (i = 0; i < N; i++) { cout << i << endl; cout << i*i << endl; if (i > M) { cout << "M exceeded" << endl; } } }
WSS White space and slashes.
- Put two blank lines and one long line of slashes before every function defintion
- Put one long line of slasshes after every function definition
- Put one blank line after the local variable declarations at the top of a function
- Put one blank line before each "//" comment that describes the purpose of a group of lines
One space should occur before and after all binary operations in an expression (except for multiplication, division and % operator). This includes the input (>>), output (<<) and assignment (=) operators. Also one space after keywords for
, while
, if
, and one space after each semi-colon in the control of a for-loop.
BN Bad name.
i
or
n
or
t
.
CN Capitalization and naming style:
Underscore Style:
Variable names, function names and new class names are all lower-case letters. Names of constants are all upper-case letters. If a name consists of several words, then these words are separated by an underscore (such as plant_age
or MANY_COLUMNS
). Functions that do not return a value (void function) should have a verb as the first part of the name (such as print_table
). Functions that return a bool value should have the word "is" as the first part of the name (such asis_inside
). Other functions that return a value should have a noun or noun phrase as the name (such as feet_per_second
).
Hungarian Notation:
Variable names, function names and new class names all have their first letter of each word capitalized. Constants are all capitals with no underscores. Any variable can have a prefix to indicate its type or purpose. Prefixes that I have seen:
b
Boolean
bool bInside;
by
BYTE (an 8-bit value)
BYTE byOffset;
c
Character
char cLetterGrade;
cb
Count in Bytes
UINT cbBuffer;
d
Double Floating-Point Number
double dAltitude;
dw
DWORD (a 32-bit value in WIN32 programming)
DWORD dFlags;
f
Floating-Point Number
float fHeight;
fn
Function
void repeat(void fnWhatToRepeat( )...
h
Handle
HANDLE hBrush;
hdc
Handle to a Device Context
HDC hdcDrawingArea;
hwnd
Handle to a Window
HWND hwndScoreDisplay;
i
Integer (32-bits in WIN32 programming)
int iPixelsPerInch;
if
Input File Stream
ifstream ifCorpus;
is
Input Stream
istream isKeyboard;
msg
A Windows Message
MSG msg;
n
An Integer That Counts the Number of Instances of Some Data
int nStudents;
of
Output File Stream
ofstream ofResults;
os
Output Stream
ostream ofScreen;
si
Short Integer
short siCents;
str
C++ String Object
string strSurname;
sz
A Null-Terminated C String
char szSurname[MAXLENGTH];
w
WORD (which is still 16-bits in WIN32 programming)
WORD wFlags;
x,y
Short used as Cartesian coordinates
short xLocation, yLocation;
struct or class name (or abbreviation)
point pointWhere;
statistician statGrades;
Certain modifiers can appear before the Hungarian prefixes. Sometimes you’ll see m_ as a prefix for a member variable, for example, though Iy dislike that one. Some others:
ar
orrg
Array (rg means "range")
double rgdScores[MANYSCORES];
l
Long
long double dAltitude;
p
orlp
Pointer (always long 32-bits in WIN32) to a single item
char* lpszWindowName;
s
Static
static int siCurrentValue;
u
Unsigned
unsigned int uiPopulation;
GV Global variable.
DC Define constant.
NC Needs comment.
If there’s something difficult about the code in a function definition, explain it in a comment near the difficult code. Long stretches of code should be broken into smaller pieces (around 6-10 lines per piece) with a short comment at the top of each section to explain its purpose. A programmer should be able to read these comments to get a general idea of a program’s flow.
The implementation file for any class requires a comment at the top to tell a programmer exactly how all the member variables are used.
UC Unnecessary comment.
CTC Code too complex.
FTL Function too long.
FP Function format problem.
return
to produce values, if you can, and use non-const reference parameters only when you have to (when the function produces several pieces of information). Avoid functions that return information in two different ways (through the
return
statement and also through its parameters).
BUI Bad user interface.
DNW Does not work.
WNC
WARN
PDR Poor data representation.
MD Misplaced declaration or definition.
NMR Needs to be more robust.
NGE Not general enough.
CF Combine functions.
PORT Portability.