CS Department
Program Style Guide

Last revised: March 2008

Programming projects will consist, usually, of programs to be written by you. While you can and indeed should work with others on the design of a homework programming assignment, the implementation should be yours alone.

Good programming style is an important feature of any good program. Gary Litvin of Skylight Software gives the following explanation on why a working program with poor programming style is not good enough:

First, a programmer's product is not an executable program but its source code. In the current environment the life expectancy of a "working" program is just a few months, sometimes weeks. On the other hand, source code, updated periodically, can live for years; to be of any use, it must be readable to others. Second, bad or unconventional style is uncool. It immediately gives away an amateur, a kind of social misfit in the software developers' culture. As nerdy as this culture might be, it has its own sense of esthetic pride.
[from The 32 Bits of Style, http://www.concentric.net/~skylit/style]

Jump directly to:

Guidelines

Documentation

See the Documentation Standards document for a description and example of program headers and for a description of code documentation.

In addition to the comments inside a program, you should create a User's Guide which gives the name of the program, directions for using it, a short description of the purpose of the program, your name, the date when you wrote the program, and dates and comments on any subsequent modifications you wrote.

Program Format

Formatting refers to the way statements are placed in a program -- indentation, blank lines, etc. Good formatting can make a program far easier to understand.

White space is essential for clarity. This includes spaces around identifiers, operators, at the beginning and end of comments, as well as blank lines, especially to separate blocks and different sections of blocks.

Consistent indentation is essential for readability. Indentation should be used to indicate that the indented statements are under the control of the previous non-indented statement. Use this as a test when deciding to indent. Always indent inside the { } bracket pair. Examples in C, C++, or Java:

Coding Style

Keep statements reasonably short. Try to avoid statements that take more than one line. Keep the number of lines in a block down so that a block fits on one screen (about 21 lines), excluding initial documentation. Then the block can be scanned easily for content, coherence, use of variables, etc.

Choose names for your variables and other identifiers that are meaningful in the context of their use.

Avoid the use of constants within program statements. Rather declare them as named constants so that a future user can change their values easily for different applications.

Capitalization

Here are the capitalization rules you are expected to use in your programs for homework, tests, and projects. Remember that many languages are case-sensitive.

Program Grading

Your programs will be graded according to the following five components: