PURPOSE
The purpose of these rules is (1) to introduce you to a representative
set of coding standards that are typical in professional programming
and (2) to help you develop good style as a habit, something you will
find necessary to successfully complete large programs. There is no
doubt that you can write C++ programs to solve the small problems in
this course without attention to style. But, you must keep in mind
that you are building professional skills, not merely finding the
answers to problems handed out as exercises.
PROPER ATTRIBUTION
Any code you use that you have not personally written must be
attributed to the author. This includes, among other things, code
from the book, code from the net, or code from your instructor.
Failure to describe where you obtained the code and the proper author
is considered plagiarism, which is cheating!
FILE NAMES AND C++ COMPILER
In this course each C++ file name should clearly represent what it is
and each should have the ``.cc'' suffix. For example, the
main file for Project 1 should be proj1.cc or
project1.cc. Keep data structures in separate files. If you
write a list class for Project 1, put the list code in
list.h, list.cc, and list.template. A lab
project that determines the payroll of a small company could be
payroll.cc
(or even something like lab3.cc). We
will use the gnu C++ compiler (g++). So, in order to compile Project
1, you would enter the command
g++ -g -Wall -Werror -O2 proj1.cc -o proj1
This produces an executable file called proj1 which you can run using
the command:
./proj1
The -g flag tells the compiler to include information needed by the GDB debugger. When given the -Wall flag, the compiler will print out every warning that it can generate for your code. The -Werror flag causes the compiler to consider every warning to be an error. Properly formatted code does not produce warnings. For most purposes, you should compile with the -O2 flag. The compiler will produce more warnings this way. However, the -O2 flag can cause unpredictable behavior in GDB, so when stepping through code in the debugger, compile with -O0 instead.
The use of Makefiles to aid the compilation process is highly recommended in general, and required for CS361/561N. Makefiles contain a set of instructions to build your program. The file is read when you type 'make' at the command line. Makefiles can save time by checking dependencies and compiling only modified code.
Each class should usually be defined in a separate file. Template functions should be in a file with a suffix of .template or .temp. Template functions are encouraged since they allow greater code reuse.
Each set of functions and/or classes that implements a different algorithm or data structure should be in a separate set of files. This allows better organization of your code, and eases code reuse.
Template files should be included at the end of header (interface definition) files (.h).
Recall that abstract data types are defined by describing the interface to the data type in the .h file, the implementation in a .cc or .template file, and are used by including the .h file.
INTRODUCTORY DOCUMENTATION
At the beginning of each file there should be a comment block that
contains:
The format to be used is as follows:
//*******************************************************************
//
// Program: program name
//
// Author: your name
// Email: your email address
//
// Lab Section: section number and instructor's name
//
// Description: brief description of the program
//
// Date: date of completion
//
//*******************************************************************
The following is an example of an introductory comment block that
follows the format above:
//*******************************************************************
//
// Program: Project 1 -- Video Calculator
//
// Author: Elwood Scuggins
// Email: Clueless@ace.cs.ohiou.edu
//
// Lab Section: 10 (Chris Hayes)
//
// Description: This program inputs the total number of minutes,
// the number of minutes for commercials, and the
// number of minutes for episodes. The program
// computes the total number of minutes for videos,
// the number of videos, and the number of seconds
// left over.
//
// Date: January 26, 2001
//
//*******************************************************************
DECLARATIONS
const float BASE = 2.0; // divisor to obtain base 2
const char HYPHEN = '-'; // signals word continued on next line
const int NAME_LENGTH = 20; // names can be 20 characters
It is permissible to capitalize the first letter of words in an identifier with class, structure, union, or type names. For example, you may wish to use Student, StudentName, or ListNode as names for your classes.
float volts; // voltage in the circuit
int amps; // amperage in the circuit
char circuit_name[NAME_LENGTH]; // name of the circuit
left = next_left; right = next_right;
use
left = next_left; // move to the left
right = next_right; // move to the right
//******************************************************************
//
// Function: find_space_cost
//
// Purpose: calculates and returns the charge for shipping cargo
// between two planets.
//
// Parameters: distance - distance in miles between two planets
// weight - weight in pounds of item being shipped
//
// Calls: function cargo_rates
//
//******************************************************************
This detailed comment should be placed in the implementation file, as which functions are called, etc. is not of general interest to someone who merely wants to use your class/function. A short version of the above comment should be placed in the .h file.
int main ()
cout << "Today's date is " << today << endl;
instead of
cout << "Today's date is " << today << "\n";
The body of a control structure, such as an if, a switch, a while, a do, or a for statement, should be indented. Always use curly braces even if there is only one statement:
if (expression) {
statements
}
if (expression) {
statements
} else {
statements
}
switch (selector variable) {
case case-1-value: case 1 statements;
break;
case case-2-value: case 2 statements;
break;
...
case case-n-value: case n statements;
break;
default: default statements;
break;
}
while (expression) {
statements
}
do {
statements
} while (expression);
for (initialization; test; increment) {
statements
}
As an alternative to the above formats, the use of the formats below
is also acceptable:
if (expression)
{
statements
}
if (expression)
{
statements
}
else
{
statements
}
switch (selector variable)
{
case case-1-value: case 1 statements;
break;
case case-2-value: case 2 statements;
break;
...
case case-n-value: case n statements;
break;
default: default statements;
break;
}
while (expression)
{
statements
}
do
{
statements
} while (expression);
for (initialization; test; increment)
{
statements
}
PROGRAM DESIGN