CMSC 202 - General C++ Coding Standards
Every programming department has some set of standards or conventions
that programmers are expected to follow. The purpose of these standards
is make programs readable and maintainable. After all, you may be the programmer who
maintains your own code more than six months after having written the original.
While no two programming departments'
standards/conventions may be the same, it is important that all members
of the department follow the same standards. Neatness counts!!!
The following standards are to be followed in CMSC 202. A separate
summary page is also available.
Part of every project grade
is based upon how well these standards are followed.
It is your responsiblity to understand these standards.
If you have any questions, ask your instructor,
any of the TAs, or the Computer Science Help Center.
C++ File Extensions
There are numerous file extensions used to designate C++
source code files and header files. For this course, the file
extensions .cpp
for source code files and .h
for header files will be used. No other extensions are permitted.
File Naming
For every project, a file containing only the main( )
function is
required. This file should be named after the project and have a .cpp file
extension. For example, the main( )
function for project 3 would be in a file named Proj3.cpp
.
Auxiliary files (e.g Proj3_aux.cpp) and/or header files
(Proj3_aux.h) are permitted, but must be named appropriately.
For most projects, you will be creating your own classes. In these
projects, the following standards also apply.
- All classes are defined in a separate header file.
Name each file after the class it contains and give it a
.h
file
extension.
- All classes are implemented in a separate source code file.
Name each file after the class and give it a
.cpp
file extension.
- No member function is to be implemented in a class header file
(i.e., no inline methods are permitted. All implementation belongs in the
matching implementation file.)
File Organization
Class Definition Standards
The following standards must be followed when a class is defined
within it's header (.h) file
- All class names begin with uppercase. Multi-word class names
follow variable/function naming convention below.
- Only one
private
, protected
and
public
section in a class definition. public
comes first, followed by protected
and then private
.
- Every data member of a class must be
private
.
- You may use global symbolic constants (
const
), but not global
variables.
- Class methods follow function naming conventions below.
- Class methods must have complete function header comments in the
header file. Fewer comments are required in the .cpp file.
- Class methods must be
const
whenever possible.
- Class data members begin with m_ and follow the variable naming
conventions below.
Example:
int m_length;
int m_nrStudents;
Variable, Constant and Function Declarations
These standards detail how variables and constants should
be declared, proper function prototype declarations as well
as naming conventions.
- Use meaningful descriptive variable names!!
For example, if your program needs a variable to represent
the radius of a circle, call it 'radius', NOT 'r' and NOT 'rad'.
- Begin variable names with lowercase letters
- The use of single letter variables is forbidden, except as simple
'for' loop indices, temporary pointer variables and in special
cases such as x- and y-coordinates.
- The use of obvious, common, meaningful abbreviations is permitted.
For example, 'number' can be abbreviated as 'num' as in 'numStudents'
or as 'nr' in 'nrStudents'.
- If commented, variables must be declared one per line. Comments
should appear on the same line without wrapping (use two lines if necessary).
- Do not use global variables
- Separate "words" within variable names with mixed upper and lowercase.
(e.g. grandTotal)
- Begin function names with uppercase letters.
- Use active function names, generally beginning with a verb
and including a noun -- GetName( ).
- Function prototypes must include parameter names as well as types.
- Functions with no parameters must include a
void
(not empty)
parameter list.
i.e. int MyFunction( ); is NOT acceptable and should
be declared as
int MyFunction( void );
- Default values for parameters must be specified in the prototype.
- Parameters must be passed by reference whenever appropriate.
- Reference parameters must be const whenever appropriate.
- Separate "words" within function names with mixed upper and lowercase.
(e.g. ProcessError)
- Constants should be commented on the same line on
which they occur. Such comments should not wrap; use two lines if necessary.
const int NUM_STUDENTS = 35; // number of students in the class
const int NUM_SECTIONS = 4; // number of sections of CMSC 104
const float CUTOFF = 88.5; // cutoff for an "A" for the 2001
// spring semester
- Constants should be used for magic numbers and strings whenever
possible. Points will be deducted from projects with code such as
int arrary[30];
for (int i = 0; i < 30; i++)
array[i] = -3;
if (command == "ADD")
// do something
else if (command == "MULTIPLY")
// do something else
Documentation
The following sections detail the required program documentation.
Failure to adhere to these standards will result in point deductions
from your project submittal.
Use of Whitespace
The prudent use of whitespace (blank lines as well as spaces)
goes a long way to making your program readable.
- Use blank lines to separate pieces of code for readability
to separate unrelated sections of code and thereby group logically related
lines of code together.
This code fragment is unreadable and results in deductions
// count total number of students
for (int i = 0; i < size; i++)
totalStudents += class[i];
cout << "total students = " << total students << endl;
while (moreStudents == true) {
// read student last name
ReadStudentName();
// print full name
PrintStudentName();
// now verify student info
goodInfo = VerifyStudentInfo(student);
if (goodInfo == true)
cout << "good info" << endl;
}
cout << " this is the end" << endl;
and should be formatted like this
// count total number of students
for (int i = 0; i < size; i++)
totalStudents += class[i];
cout << "total students = " << total students << endl;
while (moreStudents == true)
{
// read student last name
ReadStudentName();
// print full name
PrintStudentName();
// now verify student info
goodInfo = VerifyStudentInfo(student);
if (goodInfo == true)
cout << "good info" << endl;
}
cout << " this is the end" << endl;
- DO NOT use tabs (unless your text editor changes tabs to 3 or 4
spaces). Tabs may be interpreted differently by different editors.
- Use 3 or 4 spaces (not 6 or 8) for each level of indentation.
and be consistent. Too much indenting makes the code unreadable and
hastens line wrapping.
// This is an example of good formatting
if ( x > 90 )
{
statement 1;
statement 2;
}
else
{
statement 3;
}
// This is an example of too much indenting
if ( x > 90 )
{
statement 1;
statement 2;
}
else
{
statement 3;
}
// This is an example of inconsistent indenting
if ( x > 90 )
{
statement 1;
statement 2;
}
else
{
statement 3;
}
- Use spaces around all operators. For example, write
x = y + 5;
NOT
x=y+5;
- Using the emacs or xemacs editor along with the .emacs file
provided for you will automatically indent your code appropriately.
The pico editor will not do so.
Use of Braces
- Use a consistent style for braces. See the
indentation styles for
appropriate placement of braces.
- Braces are not required for single statement if/else/while/for structures,
assuming suitable indentation is used to show the structure.
For example, the following structures are permitted
if (grade > 90)
cout << "You got an A" << endl;
else
cout << "No A for you" << endl;
for (i = 0; i < 30; i++)
array[i] = -3;
Comments
Comments are the programmer's main source of documentation.
From "The Practice of Programming" by Brian Kernighan and Rob Pike, page 23
Comments are meant to help the reader of a program. They do not
help by saying things that the code already plainly says, or by
contradicting the code, or by distracting from the code with
elaborate typographical displays. The best comments aid the understanding
of a program by briefly pointing out salient details or by providing
a larger-scale view of the proceedings".
Commenting standards for files, functions, and code are described below.
File Header Comments
EVERY .cpp and .h file should contain an opening comment
describing the contents of the file and other pertinent information.
This "file header comment" MUST include the following information.
- The file name
- The project number
- Your name
- The date the file was created
- Your section number
- Your UMBC e-mail address
- A description of what the code in the file does
For example,
/*****************************************
** File: Proj1.cpp
** Project: CMSC 202 Project 1, Spring 2003
** Author: Bob Smith
** Date: 6/22/99
** Section: 0304
** E-mail: bsmith32@gl.umbc.edu
**
** This file contains the main driver program for Project 1.
** This program reads the file specified as the first command line
** argument, counts the number of words, spaces, and characters, and
** displays the results in the format specified in the project description.
**
** Usage: Proj1
**
***********************************************/
Function Header Comments
EACH FUNCTION and CLASS METHOD must have a header comment that
includes the following:
- The function's name
- The function's pre-condition(s) (if there are no
pre-conditions, say so).
- The function's post-condition(s).
A pre-condition is a statement giving the condition(s)
that is (are) required to be true when the function is called. The function
is not guaranteed to perform correctly unless the pre-condition
is true. (see text page 113)
All functions must test for all pre-conditions.
Until we learn about exceptions, if a false pre-condition
can be handled in code, do so (see CircleArea function below).
If it is fatal, use assert().
A post-condition is a statement describing what will be
true when the function call is completed (assuming the pre-condition is
met and the function completes). (see text page 113).
For example,
/************************************************
** Name: CircleArea
** PreCondition: the radius is greater than zero
** PostCondition: Returns the calculated area of the circle
** **************************************************/
double CircleArea ( double radius )
{
const double PI = 3.14159;
if (radius < 0.0)
return 0.0;
else
return PI * radius * radius;
}
In-Line Comments
- In-line comments are used to clarify what your code does,
NOT how it does it.
- An in-line comment MUST appear above the code to which it applies
and is indented to the same level as the code.
The following example is acceptable.
// check every element of the array
for (i = 0; i < ARRAYSIZE; i++)
{
// add one to odd values
if (array[i] % 2 == 1)
{
array[i]++;
}
}
The following example is not acceptable
for (i = 0; i < ARRAYSIZE; i++) // check every element of the array
{
if (array[i] % 2 == 1) // add one to odd values
{
array[i]++;
}
}
- Be sure your comments don't contradict the code
- Don't comment bad code -- rewrite it
- Don't comment the obvious. Comments like these are worthless
// return true
return true;
// increment counter
++counter;