This style guide is mandatory for all submitted work for grading (homework, projects, labs, exams). The purpose of the guide is not to restrict your programming but rather to establish a consistent style format for your programs.

Whitespace and 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. There are three major ways we use whitespace to format code for maximum clarity:

Bad Formatting
#include <iostream>
using namespace std;
int main()
{
// Read first number
double x;
cout<<"Enter first number : ";
cin>>x;
// Read second number
double y;
cout<<"Enter second number: ";
cin>>y;
// Calculate sum
double sum;
sum=x+y;
// Print sum
cout<<"The sum is: "<<sum<<endl;
return 0;
}
Good Formatting
#include <iostream>
using namespace std;

int main()
{
  // Read first number
  double x;
  cout << "Enter first number : ";
  cin >> x;

  // Read second number
  double y;
  cout << "Enter second number: ";
  cin >> y;

  // Calculate sum
  double sum;
  sum = x + y;

  // Print sum
  cout << "The sum is: " << sum << endl;

  return 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.
  • Filename and Author should be given in the beginning of your code.
  • Strategic comments are used to give the reader a general overview of what is going on. These comments appear at the beginning of files, before important functions, and above important blocks of code. Strategic comments tend to be written in sentences with an emphasis on explaining the big picture of what the block of code is designed to do.
  • Tactical comments are designed to explain tricky areas of code, what parameters do, and hints about the control flow of the program. These comments should be intermixed in the code when needed. They should be shorter than strategic comments, in bullet format, and may be in inline format. One should, for instance, place a tactical comment next to the statement that assigns a fixed, non-obvious value to a variable.
The following example shows good commenting. Note the tactical comment about why "9.0" is used instead of "9".
/***************************************************
Filename: f2c.cpp
Author: MIDN Nobody Smith (259999)
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;
}

Naming

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.
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;

Other Tips on Naming

Miscellaneous but Important Stuff

Practical tips for tackling programming problems

Here are a few practical tips that will improve your programming skills.