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.
"Whitespace" refers to newlines, space characters and tabs. In source code we do not generally use tabs — in fact, most editors replace 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 following is a perfectly valid program:
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); } |
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) |
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 but 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.
Good | Good (Preferred) | Bad | Bad |
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 should be indented consistently with the block of code they describe. Comments can be divided into two general categories, strategic and tactical.
main
function. Note the tactical comment about why "9.0" is used instead of "9".
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; }
Functions and variables should have meaningful names that make your code easier to understand and reduce the need for comments. This technique is known as Self-Documenting Code, . For example, consider a program that uses a variable to track the yards per carry of a football game. 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 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, in general, should be avoided. One exception to this rule is when it is common practice to identify something with a single letter, for example, use of 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 words (a word phrase) SHALL have each word in the phrase distinguished by capitalizing the first letter of the the 2nd - nth words in the phrase (e.g. yardsPerCarry) or by seperating words in the phrase with underscores (yards_per_carry). The former method is also known as Camel case (specifically lower camel case) and is strongly encouraged. Constants should be named in all upper case letters. Example: const double 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 "cel1
" c - e - l - ONE
or c - e - l - l
?
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 should be verbs or verb phrases that 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.
Each file must have a descriptive comment header block at the top of the file as follows:
/****************************************************************************** File: ___________ <name of this file, .h or .cpp> Name: ___________ <your name> Course/Section: SI221/1121 Project | HW | Lab: ___________ <name of the assignment> Date: ___________ <dd mmm yyyy> Description: A brief description for the purpose of this file, what it accomplishes, and what function it performs. Resources used: List all resources used except the course website. If none, state so. Help received: List all help received, include the name of the individual and topic. If none, state so. Help given: List all help given, include the name of the individual and topic. If none, state so. ******************************************************************************/
Each function must have a descriptive comment header block above the function declaration/prototype AND it's definition/implementation. For example:
In .h File:
/** Function: productFuntion(double x, double y) Purpose: Returns the product of x and y as a double. **/ double productFunction(double x, double y);
In .cpp File:
/** Function: productFuntion(double x, double y) Purpose: Returns the product of x and y as a double. **/ double productFunction(double x, double y){ return x * y; }
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.
Original Coding Standard was developed by Prof Christopher W Brown.