| CS150 | Lab | Projects | Assignments | CodeSamples | Help Resources | Style Guide |
Software developers should write code that is:
Coding standards and guidelines help to achieve these goals.
Many of the following standards and guidelines are derived from the reference books C++ Programming Guidelines
and Effective C++.
/*
filename: make_change.cp
author: S. Gordon
date last modified: 8/17/99
This program accepts a dollar amount specified by the user and displays the minimum number of
$10, $5, and $1 bills required to make up the amount. */
fahrenheit_to_celsius
:
double fahrenheit_to_celsius( double F );
//Receives a Fahrenheit temperature F. Converts F to Celsius and returns the result.
// Precondition: F is a temperature expressed in degrees Fahrenheit.
// Postcondition: Equivalent temperature of F in degrees Celsius is returned.
int total_cost, item_count; //total cost of item_count purchases
double average_cost; //average cost of an item, to be computed
You may also use a comment to precede a block of code:
//perform linear search of costs array for a matching value
int i = 0;
while ( i < SIZE && costs[i] != search_val )
i++;
if ( x > MAX )
{
cout << "x is larger\n";
x = MAX;
}
else
{
cout << "x is smaller\n";
x = 0;
}
Alternative style:
if (x > MAX) {
cout << "x is larger\n";
x = MAX;
}
else {
cout << "x is smaller\n";
x = 0;
}
if
statements tend to quickly migrate to
the right following a simple indent each control structure approach. If you have a situation which requires multiple
nested if
statements, instead of doing:
if ( dayOfWeek == 1 )
// do this;
else
{
if ( dayOfWeek == 2 )
// do that;
else
{
if ( dayOfWeek == 3 )
// do something else;
etc...
Do this instead:
if ( dayOfWeek == 1 )
// do this;
else if ( dayOfWeek == 2 )
// do that;
else if ( dayOfWeek == 3 )
// do something else;
Or, use a switch
statement. Either will save
space and be easier to read, while producing the same results as the nested if
s.
void AddNumbers( double, double ); void MultNumbers( double, double ); void OutOpenMessage(); char InCommand();
-space here
-space here
int main() { double firstDouble = 4.2; double secondDouble = 6.5; char command; bool valid = false;
-space here -space here
OutOpenMessage(); // outputs opening screen message
-space here
do { command = InCommand(); //returns user input command
-space here
switch( toupper( command ) ) { case 'A': AddNumbers( firstDouble, secondDouble ); //outputs sum valid = true; break;
-space here
case 'M': MultNumbers( firstDouble, secondDouble ); //outputs product valid = true; break;
-space here
default: cout << "Input error" << endl; } } while( !valid );
-space here
return 0; }
-space here -space here
void AddNumbers( double first, double second ) { double sum;
-space here
sum = first + second; cout << first << " + " << second << " = " << sum << endl; }
#ifndef TIMEFILE_H
#define TIMEFILE_H
const int MAX_LENGTH = 10;
FinalScore
finalScore
final_score
bool notValid; // Avoid this
bool valid; // Use this affirmative form instead
If variables are named to assert the negative, then somewhere code will
end up looking like this:
if(!notValid) // Double negative is confusing