PURPOSE
The purpose of these rules are (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.
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, Project 1
should be proj1.cc or project1.cc. 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 g++ compiler. So, in order to compile
Project 1, you would type the command
g++ proj1.cc
INTRODUCTORY DOCUMENTATION
At the beginning of each program there should be a comment block that
contains:
The format to be used is as follows:
//*******************************************************************
//
// Program: program name
//
// Author: your name
//
// Recitation 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
//
// Recitation Section: 10 (Daniels)
//
// 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: September 26, 1997
//
//*******************************************************************
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
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 // //******************************************************************
void main ()even though that will lead to a warning from some C++ compilers.
float expo (float value, int exponent);But, it is also permissible to use only the parameter types since the C++ compiler ignores the names:
float expo (float, int);
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. All of the following are acceptable formatting styles:
if statement
if (expression)
{
statements
}
if (expression)
{
statements
}
if (expression) {
statements
}
if-else statement
if (expression)
{
statements
}
else
{
statements
}
if (expression)
{
statements
}
else
{
statements
}
if (expression) {
statements
}
else {
statements
}
switch statement
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;
}
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;
}
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 statement
while (expression)
{
statements
}
while (expression)
{
statements
}
while (expression) {
statements
}
do-while statement
do
{
statements
}
while (expression);
do
{
statements
}
while (expression);
do {
statements
} while (expression);
for statement
for (initialization; test; increment)
{
statements
}
for (initialization; test; increment)
{
statements
}
for (initialization; test; increment) {
statements
}
PROGRAM DESIGN
Return to homepage of CS 180 -
Introduction to Computer Science