Chris Pollett>Old Classes>CS112, Spring 1998>Coding Conventions

Introduction to Computer Science II
Spring 1998


Programming Conventions
(Coding Guidelines)


Last Modified:
Mon Jan 20 08:01:47 1998


This is a compulsory set of programming rules to foster the habit of following some convention consistently. These conventions are designed to improve code readability, on the premises that far more human time is spent reading code than writing it, that far more human time is spent modifying existing code than writing entire programs from scratch, that far more code is written by groups of people than by single individuals. In particular, following these rules will make it easier for the grader to grade your work and the instructors to assist you if you are having difficulty.

This convention is modified from the conventions used in Randy Pruim's CS113 class for Fall 1997. When there is a difference between what is done in the books and what is outlined here, this takes precedence. If you have a question about something not covered here, imitate the code from Standish.

Names

As part of program documentation, names must clearly indicate both the kind of thing being named and its role in the program.

In addition to following the guidelines above to indicate what kind of thing you are naming, a name should be chosen which will reveal the meaning or use of that thing in your program.

Indentation

Maintain consistent indentation throughout your code. Examples:


if (condition){
    statements
} else {
    statements
}			/* end if ... */


for (...){
    statements
}  			/* end for ... */

Rightward Drift. As you get more and more deeply nested block of code, indentation pushes you farther and farther to the right, until eventually you have hardly any room to write a line of code. To avoid this, you can do two things:

Comments

Comments should help the reader of your code to understand it. In the case of a programming course, they also serve to convince the grader that you grasp the what, how and why of your code (as opposed to having hacked it into working). Like any other form of expression, it should not be flowery or repetitive. In particular observe the following guidelines

/*
 * Function: FunctionName
 * Usage: t = FunctionName(proposedName) 
 * -------------------------------------
 * This function returns a boolean value which is true if proposedName is
 * a name contains a string which follows the guidelines given in this 
 * convention for naming functions, otherwise it returns false.
 *
 * In general, comment here on anything which would be of use for the 
 * _caller_ of the function. You may comment on the types of the variables 
 * (as I did in * the paragraph above).  Sometimes this may be unnecessary, 
 * if it is clear from the name of the function and its parameters and the 
 * comments are not too lengthy, since the prototype will follow.  Other times 
 * it might be nice to list the variables and what they mean, just as you
 * would in the main program.
 */

boolean FunctionName(string proposedName);

Standard Boiler Plate

Each source file must have a standard "boiler plate". This boiler plate must include your name, the class (CS 112), the assignment number, due date, the problem number, and a brief description of the what the code in the file does. You should outline the method you used to solve the problem, describe the variables and data-structures used in the program, any error conditions which might be encountered in the solution, and how these error conditions are handled by the solution, any known deficiencies (documented bugs, cases which the program does not handle, etc.), and a separate list of enhancements (any features not required in the assignment) if there are any.

Specific Guidelines for the Boiler Plate:

Example

Here is a simple example of the program ave2f.c with a boiler plate included. (This program appears on page 47 of Robert's The Art and Science of C.) For a simple program such as this one, some of the comments may seem like overkill, but it will become increasingly important as our programs become more complicated.

You may imitate this style (here is a blank version which you can edit for your programs) or feel free to make up your own boiler plate style. If you decide to use your own boiler plate, use it consistently and be sure it includes everything required.

Avoid Global Variables!

Before you use global variables, consider first supplying pointers to variables so that functions can modify those variables by dereferencing the pointers. If that is not enough, then try static local variables, and, failing that, static global variables. Only when none of these work--a very rare event indeed--should you resort to plain global variables.

If you do use a global variable (plain or static) be sure to document carefully when, where (in which blocks of code), and why the variable is changed. One of the most difficult things to debug can be the overuse or improper use of global variables.

Keep Line Lengths Under 80 characters

On many machines and in many software packages, this is a standard line length. It makes it easier for others (like graders) to read and/or print your programs.

Make judicious use of White Space

Sometimes the simple insertion of a blank line here and there to help group a few lines of code which work together to achieve a subgoal can make the program much easier to read. But don't put in too much white space or the reader will not be able to see enough of the code at a time.