Handout 0

CS340 Programming Standards*


Requirements

Comments:

  1. Every file must have a comment header at the top of the file, which includes:
  2. Every function and class (including the main function) must include comments which describe:

Variable Names:

  1. Use self-documenting identifier names (for constants, variables, types, subprograms, ...).
  2. No global variables (but global named constants are permissible).
  3. Use named-constants rather than literal-constants (if a value will not change during run-time assign it to a constant rather than a variable).
  4. Use appropriate data types (such as declare all index variables as integers, not as a floating-point type).
  5. Use language constructs that correspond to the logical structure.
    1. Bad Example:
         if (hoursWorked < 40)
            pay = hoursWorked*hourlyRate;
         if (hoursWorked >= 40 && hoursWorked < 60)
            pay = 1.5*hoursWorked*hourlyRate - 20*hourlyRate;
         if (hoursWorked >= 60)
            pay = 2*hoursWorked*hourlyRate - 50*hourlyRate;
    2. Good Example:
         if (hoursWorked < 40)
            pay = hoursWorked*hourlyRate;
         else if (hoursWorked < 60)
            pay = 1.5*hoursWorked*hourlyRate - 20*hourlyRate;
         else
            pay = 2*hoursWorked*hourlyRate - 50*hourlyRate;
  6. Variable and function names:

Indenting

For block structured procedural languages like C, Pascal or Ada, the body of a loop (while loop, for loop, do loop, repeat until loop or any other looping construct) should be indented. Code that is conditionally executed (from an if statement or other decision statement) should be indented. The level of indentation should be equal to the nested depth of the code.

Comments

    Use comments generously to tell in English what your program is doing. They are to be used to describe the purpose of a program (in the header from guideline #1), of each defined variable and constant macro (see guideline #3), of each function within the program (see text), and of any other key program segment. However, comments must be meaningful, that is, quality is as important as quantity. For example, vacuous comments like

    			a = b + c;   /* Add b and c and store in a */
    
    
    tell us nothing of value and should be avoided (they also do NOT make up for the lousy choice of variable names!).

Spacing

Goals:

  1. The goal of a comment is to explain; "what the program does, not how it is done".
  2. Use appropriate data structures.
  3. Use appropriate algorithms.
  4. Each programmer-defined ADT should be implemented within a separate file (which includes data representations and method implementations).
  5. Functions should not be more than one printed page long (about 60 lines).

Example (code available here)

// filename: avg.cpp
// assignment: MP05
// author: Ada Lovelace
// due date: March 25, 2525
// course: CS260
// semester: Spring, 2525
// IDE: Microsoft Visual C++ 6.0
// description: The avg function returns the arithmetic mean of a vector of 
//              numvalues items. If numvalues is less than 1, the result is 0.

#include <math.h>

// function prototypes
float avg(float [], int);

// Define program limits
const int MAXNUM = 100;

// The avg function returns (float) the arithmetic mean of a vector of 
// numvalues items. If numvalues is less than 1, the result is 0.

float avg( float x[MAXNUM],        // in: array of data values
           int numvalues ) {       // in: number of values in x 

        float sum;      // sum of the data values  
        int     i;      // index into input array x  

        if (numvalues > 0) {    // check for empty vectors 
                
                sum = 0.0;      // initialize sum 

                for (i = 0; i < numvalues; i++)
                        sum += x[i];    // sum all values in array x 

                return (sum / (float)numvalues);
        }

        else    // mean of nothing is zero 

        return (0.0);
}


* portions of these standards are from here, here, here, here.