Program Guidelines
/*
Program 1: Find the Missing Link
Programmer: Dan Bennett
Class: CSCI 000, Spring 2001
Short Description: This program simulates the evolution of
species to find the missing link between ape and man.
Narrative:
This program ...
Special Notes:
* If you run this program with the -G flag, the program will
simulate the presence of God, and thus no missing link will be
found, man is just created.
* This program ...
*/
/*
Module: Genesis
Input:
days: an integer specifying the number of days
species: an integer specifying the number of different
species to generate
Input/Output:
world: a two dimensional array of species, empty on
input, populated on output.
Output:
done: boolean, true if the creation succeeded, false if
it fails for any reason.
This module will simulate the creation according to the chapter of
Genesis. It will ....
*/
Acceptable: worldArray, numberOfSpecies, currentYear
Poor: a, b, wrdary, foo, bar, foobar
worldArray, numberOfSpecies for variables
Genesis, GrowTheTrees for functions/procedures
TOTAL_YEARS, MAXIMUM_TIME for constants
const int TOTAL_YEARS = 10000;
This is a tricky rule, if a number has a meaning, make a name for it.
If it does not, don't bother. For example, there is no reason to name a
constant for formatting output. The following is definitely useless and,
to me, somewhat annoying.
const int FIVE=5
// prompt the user for a
cout << "Input the length of side A -> " ;
// read in a
cin >> sideA;
// print the end of line
cout << endl;
// get the value of the second side.
cout << "Input the length of side B -> " ;
cin >> sideB;
cout << endl;
// let c be the square root of a*a + b*b
hypot = sqrt(sideA*sideA+sideB*sideB);
// use the Pythagorean Theorem
hypot = sqrt(sideA*sideA+sideB*sideB);
You should tell the user what you want. Both in terms of value and of type. Unless instructed otherwise, you may assume that the users of your program know the types and will follow your instructions for input.
I tend to violate this rule somewhat. I prefer this:
if("cat" == critterType) {
return (1);
} else if ("dog" == critterType) {
return(2);
} ...
to this:
int returnValue;
returnValue = 0;
if("cat" == critterType) {
returnValue = 0;
} else if ("dog" == critterType) {
returnValue = 1;
} ...
return returnValue;
Smaller more readable modules are preferred. Never have huge blocks of code repeated.
cout << "Enter your First Name =>"; cin >> firstName; cout << endl; cout << "Enter your Last Name =>"; cin >> lastName; cout << endl; cout << "Enter your Middle Name =>"; cin >> middleName; cout << endl; cout << "Enter your Title =>"; cin >> titleName; cout << endl; cout << "Enter your Nickname =>"; cin >> nickName; cout << endl;I would prefer the following:
/*
This function prompts the user for a string. It will read the
string into a single string variable, clean up the input stream
and return the value
Input:
The name of the the string the user wants (part of the prompt)
Output:
The string the user typed. No type checking is performed
*/
string PromptForString(string prompt) {
string tmp;
cout << "Enter your " << promt << " => ";
cin >> tmp;
cout << endl;
return tmp
}
...
// ask the user for a bunch of name related information
firstName = PromptForString("First Name");
lastName = PromptForString("Last Name");
middleName = PromptForString("Middle Name");
title = PromptForString("Title");
nickName = PromptForString("Nickname")