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. 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. Points will be deducted from submitted work that does not conform to this style guide.

Visual Layout

Bad 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;}
"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, the left code is a perfectly valid program but pretty hard to read.

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;
}
There are three major ways we use whitespace to format code for maximum clarity:
  • Whitespace separating elements of code: Consistent use of whitespace transcends blank lines and indentation, it also looks at how spaces are used to separate elements of a line of code. The style guide recommends whitespace between operators and operands, except where precedence is highlighted by leaving out spaces.
    Good y = 2*y + x/y + 5; if (y >= 2*y - 5 && x*y <= 24)
    Bad y=2*y+x/y+5; if (y>=2* y-5&&x * y<=24)
  • Blank Lines: In source code, use blank lines to separate chunks of code that accomplish distinct tasks. While to some extent this is taste and art, you should expect distinct functions to be separated by blank lines, and a long function body should usually be broken into chunks by blank lines as well.
  • Indentation: Indentation highlights the block structure of programs, much as it does in formatting outlines. Each level of block nesting must be indented by a consistent number of spaces (two in the emacs settings we provide you). New levels of indenting are introduced by function bodies, for, while and do-while bodies, the then and else blocks of an if statement, and struct definitions.

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 (preferred for this course) 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. Generally, curly braces of either kind will occupy their own line, except for rare occasions when an entire block is put on one line. 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;

Comments

Comments should be indented consistently with the block of code they describe. Comments can be divided into two general categories, strategic and tactical.
  • 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".
/***************************************************
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

Miscellaneous but Important Stuff

Practical tips for tackling programming problems

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

Tips for managing your time

At the end of the 2nd week, you must know how long it take you to finish the following on average: Then, you need to allocate enough of your time in your schedule in order to be successful in this course.
There are three projects in this course. As a rule of thumb, you will need 3-6x of lab time for finishing a project.