C++ Coding Standards

Most organizations that produce computer code have a set of standards or conventions that programmers are expected to follow. The purpose of these standards is make programs readable and maintainable. While no two organizations' standards are identical, it is important that all programmers within an organization know and follow the same standards. The following standards are to be followed in CMSC 202.

A portion of every project grade is based upon how well these standards are followed. It is your responsibility to understand these standards. If you have any questions, ask your Instructor or a TA.

File Names and Extensions

There are multiple 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 shall be used. No other extensions are permitted.

For every project, a file containing the main() function is required. This file shall be named after the project and have a .cpp file extension. For example, the main() function for Project 3 should be in a file named proj3.cpp.

Auxiliary files (e.g proj3aux.cpp) and header files (e.g. proj3aux.h) are permitted, but must be named appropriately.

The executable for a project shall be named after the project. For example, the executable for Project 3 must be named proj3. This is very important because the graders will look for that file to run your program. The graders will not run a.out or any other executable. The executable name is controlled by your Makefile — get it right.

For most projects, you will be creating your own classes. For these projects, the following standards also apply:

File Organization

Class Definition

The following standards must be followed when a class is defined within it's header (.h) file:

Variable, Constant, and Function Declarations

These standards detail how variables and constants should be declared, proper function prototype declarations, and naming conventions.

Documentation

The following sections detail the required program documentation. Failure to adhere to these standards will result in point deductions from your project submission.

Use of Whitespace

The prudent use of whitespace (blank lines as well as spaces) goes a long way to making your program readable.

Use of Braces

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.

Rule of Thumb: Every five lines of code needs at least one comment. Not every line of code needs a comment. Constant declarations must have one comment.

File Header Comments

Every .cpp and .h file shall contain an opening comment describing the contents of the file and other pertinent information. This "file header comment" must include the following information.

  1. The file name
  2. The project number
  3. Author's name
  4. The date the file was created
  5. Author's section number
  6. Author's UMBC e-mail address
  7. A description of what the code in the file does

For example:

/*****************************************
** File:    Proj1.cpp
** Project: CMSC 202 Project 1, Fall 2005
** Author:  Bob Smith
** Date:    9/22/05
** 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.
**
**
***********************************************/

Function Header Comments

Function header comments are the primary form of documentation for the user of our functions and classes. It is important that this documentation be both complete and accurate as it forms a "contract" between the user and and the implementer.

Each function and class method shall have a header comment that includes the following information:

  1. The function's name
  2. The function's pre-conditions (if there are no pre-conditions, say so)
  3. The function's post-conditions

The full function header comment must appear in the appropriate .h file.

A pre-condition is a condition that is required or assumed to be true when a function is called. The function is not guaranteed to perform correctly unless the pre-condition is satisfied. It is not just a restatement of the parameter names and types. All functions must test for pre-conditions to the extent possible.

A post-condition is a condition that will be true when the function is completed, assuming the pre-conditions for the function have been satisfied and the function completes.

For example, in Circle.h we would expect to find prototypes and full function header comments as follows:

//-------------------------------------------------------
// Name: CircleArea
// PreCondition:  the radius is greater than zero
// PostCondition: Returns the calculated area of the circle
//---------------------------------------------------------
double CircleArea (double radius ):

whereas in the .cpp file we expect to find the function implementation and comments meant for the programmer:

// CircleArea
// Given the radius, returns the area
double CircleArea ( double radius )
{
   const double PI = 3.14159;

   // handle unmet precondition
   if (radius < 0.0)
      return 0.0;
   else
      return PI * radius * radius;
}

Pre- and post-conditions will be discussed in more detail in class.

In-Line Comments