1.0 General

This style guide is mandatory for all submitted work for grading (homework, projects, labs, exams). Points will be deducted from submitted work that does not conform to this style guide. The purpose of the guide is not to restrict your programming but rather to establish a consistent style format for your programs. This will help you debug and maintain your programs, and help others (including your instructor) to understand them. As your programs grow in length and complexity, it is critical to use a consistent style. In the long run this will help you since one focus of this course is to build a foundation for later courses.

This list of requirements may seem long, tedious, and beside the point. It's certainly the first two. But, I assure you by the end of the semester, you'll agree these are good rules that made things better in the long run. And besides, you're getting off cheap - here's Google's C++ Style Guide.

2.0 Visual Layout

"Whitespace" refers to newlines, space characters and tabs. In source code we do not generally use tabs — in fact, emacs replaces tab characters with an appropriate number of space characters. In many circumstances whitespace is not required in order for compilers to correctly compile source code, even though the result is unreadable to us. For example, each of the following is a perfectly valid program from the point of view of the compiler...

Bad Formatting Good Formatting
#include <iostream>
using namespace std
;int main(){int x,y
=1;cin>>x;while(x>0
){y=2*y;x--;}cout<<
y<<endl;return(0);}
#include <iostream>
using namespace std;

int main() {
  int x, y = 1;
  cin >> x;

  while(x > 0) {
    y = 2*y;
    x--;
  }

  cout << y<<endl;
  return(0);
}

... but they are not equally easy to read. There are three major ways we use whitespace to format code for maximum clarity:

3.0 Curly Braces, i.e. { }'s

Curly Braces, i.e. { }'s in C++ are used to create a "block" from one or more statements. The delineate the bodies of functions, loops and switch statements, struct definitions, and the then and else blocks of if statments. Generally, one should either always put the opening curly brace on a new line or always put it last on the previous line. Once again consistency is key. If the opening brace goes on a new line, the preference for this course is to keep the previous line's indentation — i.e. do not indent the curly brace. The exception is that comments may follow either opening or closing braces.

GoodGoodBadBad
while (x < 3)
{
  cout << x << ",";
  x++;
}
cout << endl;
while (x < 3) {
  cout << x << ",";
  x++;
}
cout << endl;
while (x < 3)
{ cout << x << ",";
  x++; } 
cout << endl;
while (x < 3)
{ cout << x << ",";
  x++;
} cout << endl;

4.0 Comments

Comments should be indented consistently with the block of code they describe. Comments can be divided into two general categories, strategic and tactical.

The following example shows good commenting. Note the tactical comment about why "9.0" is used instead of "9".

/***************************************************
Fahrenheit to Celsius Conversion
***************************************************/
#include <iostream>
using namespace std;

int main() {
  // Read temperature in Fahrenheit
  double Tf;
  cout << "Enter temperature in Fahrenheit: ";
  cin >> Tf;

  // Compute temperature in Celsius
  double Tc;
  Tc = (Tf - 32)*(5/9.0); // .0 forces division as double

  // Write temperature in Celsius
  cout << "That is " << Tc << " degrees Celsius." 
       << endl;

  return 0;
}

5.0 Naming

Variables should have meaningful names. For example, consider a program that uses a variable to track the yards per carry of a football player. Such a variable should be called yardsPerCarry vice ypc for program readability. This must be balanced against using names which are too long, which can obscure the code. (Fifteen or so characters approaches the “too long” limit.) It should be mentioned that this is most important for variables that have large scope — i.e. they are visible and need to be used either from different files or from widely separated lines in the same file. The code example above seems to violate this rule, but variables Tf and Tc only a appear, and are only in scope, within less than a dozen lines of code, and in the context of those few lines they are descriptive enough. Also, because this is an example embedded in a document, compactness of the code is very important.

Single letter variables or constants should not be used with the following exceptions. An exception to this rule is when it is common practice to identify something with a single letter. An example of this is the coordinate system (x, y, and z). A second exception occurs in the use of loop counter variables where it is common practice to use variables like i and j as counters in for loops.

Function names and variable names should begin with a lower case letter. An identifier consisting of multiple names SHALL have each name distinguished by making the first letter of each name part (after the first) upper case (e.g. yardsPerCarry) or by using underscores (yards_per_carry). The former method is strongly encouraged. Constants should be named in all upper case letters. Example:

const int PI = 3.14;

It's generally good to avoid lower case letter ‘L’, or the letter ‘O’ in names unless they are part of normal words. This is to avoid confusion with the numbers 1 and 0. For example, is "cell" c- e - 1- ONE or c- e- 1-1?

Avoid names that differ only in case, look similar, or differ only slightly. For example, InputData, InData and DataInput will certainly be confusing if used in the same program.

Names of functions should reflect what they do (printArray), or what they return (getAge). Boolean variable names should sound like Yes/No things — "isEmpty" and "isFinished" are possible examples.

6.0 Miscellaneous but Important Stuff

No line of code should wrap beyond the right margin. If a line of code becomes too long, break it up into pieces. Remember the compiler largely ignores extra whitespace in your programs (but be careful with strings).

Always use the most appropriate operator or construct for the work you want it to do. Good design and clarity take precedence over optimization. Do not declare or use more variables than are necessary.

Numerical constants ("magic numbers") must not be coded directly. The only allowable exceptions are for 0, 1 or -1, and those which are not likely to change; for example code determining if a number is even can use the number 2 since it is not likely to change. Numerical constants must have a comment explaining the value if it is not evident from the name.