Compile with "-Wall" and remove all warnings.
Include a comment block at the top of EVERY file, which includes the filename, author, course with section number, assignment, and a description of the program or what the file contains. Note the SPACING and ORDER.
/* Filename : Primes.cc Author : Gary M. Zoppetti Course : CSCI 362-01 Date : 01/10/2021 Assignment : Program 2, part A (be specific) Description: Calculate prime numbers between 2 and N (use your own words)... */
Use BLANK LINES between blocks of code to enhance readability. Think of the code as if you are writing paragraphs in a paper. When your code shifts to a “new thought,” place a blank line.
Use section SEPARATORS of some sort. Separate function definitions using a separator.
/************************************************************/ // System includes #include <iostream> ... /************************************************************/ // Local includes #include "HighResTimer.hpp" ... /************************************************************/ // Using declarations using std::cout; ... /************************************************************/ // Function prototypes/global vars/type definitions using uint = unsigned int; void radixSort (VectorType& v, uint maxDigits); /************************************************************/ int main (int argc, char* argv[]) { ... } /************************************************************/ void radixSort (VectorType& v, uint MaxDigits) { ... }
Place return types on a line by themselves for function (method) declarations and definitions. Include parameter names in both.
// Declaration (prototype) float calcInterest (float balance); ... // Definition float calcInterest (float balance) { // body }
Place curly braces on a line by themselves.
if (isSorted) cout << "naturally"; else { cout << "oops -- user error?"; exit (1); }
Include a space before left parentheses. If the left parenthesis is starting a parameter or argument list the space is optional (I still include it). Flank binary operators with spaces.
interest = calcInterest (balance); // OR interest = calcInterest(balance); int a = 3 * (4 + 5); // NOT int a=3*(4+5)
Use between 2 and 4 spaces for each level of indentation. Be CONSISTENT.
Use Java naming conventions (lowerCamelCase and UpperCamelCase) and choose meaningful, descriptive names. If a value with units is represented, include the units in the name. Constants should be in all CAPS, with underscores used to separate words.
Avoid using SINGLE LETTER names except when there is no easy way to create a variable name that is descriptive. For example, "s" is OK for the name of a String if you are referring to any generic string and not something specific such as a word or name (in which cases "word" or "name" would be preferable).
float rotationAngleRadians; const unsigned NUM_SORT_VALUES = 10'000; // Start method names with a verb to convey action void convertToLower (string& s); // Use nouns for class names class Dictionary;
Use pre-increment and pre-decrement instead of the postfix versions.
++i; --j;
Define "main" BEFORE any other functions. This will require you use prototypes.
Don't write useless comments.
// Loop while "cond" is false while (! cond) { ... } // end while // Make "i" point to the beginning element i = v.begin ();
Write comments ABOVE the line you are commenting.
// Comment here sort (v.begin (), v.end ()); sort (v.begin (), v.end ()); // NOT here