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 ... */
/* Function: 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 function will simulate the creation according to the chapter of Genesis. It will .... */
// Acceptable: string worldArray; int numberOfSpecies; long currentYear; // Poor or unacceptable: int a, b; string wrdary, foo, bar, foobar;
// variables string worldArray; int numberOfSpecies; // functions void Genesis(...); int GrowTheTrees(...) ; // constants const long TOTAL_YEARS = 2000000; const long MAXIMUM_TIME = 1000000000;In addition, I like to end types with a T, this indicates type.
class SpeciesT { ... }; typedef unsigned char MonthT;
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);
// unacceptable int value; int max, min; int count=0; cin >> value; max = value; min = value; while (cin) { if(max < value) { max = value; } if (min > value) { min = value; } count ++; cin >> value; } // ********************************************************************* // acceptable int value; int max, min; int count=0; cin >> value; max = value; min = value; while (cin) { if(max < value) { max = value; } if (min > value) { min = value; } count ++; cin >> value; }
// not a proper solution to fizzbuzz but I wanted single line nested if. int main() { int count; int i; cout << "Enter a positive integer :"; cin >> count; for(i=1; i <= count; i++) { if (i%3 == 0) { cout << "Fizz"; } else if (i%5 == 0) { cout << "Buzz"; } else { cout << i; } cout << endl; } return 0; }
// not a proper solution to fizzbuzz but I wanted single line nested if. // while this takes up less space, I feel it is more error prone // you should not use this style in my classes. int main() { int count; cout << "Enter a positive integer :"; cin >> count; for(i=1; i <= count; i++) { if (i%3 == 0) cout << "Fizz"; else if (i%5 == 0) cout << "Buzz"; else cout << i; cout << endl; } return 0; }
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 = 1; } else if ("dog" == critterType) { returnValue = 2; } ... 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 " << prompt << " => "; 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")