CS 302 Program Style Guide

Program Style Guide

In This Page: Braces | Whitespace | Indentation | Naming     Related Pages: Commenting Guide | Example

Good programming style isn't meant for the compiler, which only requires that you use the correct syntax for the programming language. Programmers use good programming style to ensure that their source code is easily read and understood by people. These people typically are members of a program development team and others who maintain or enhance the code. Good style is particularly important when an author leaves a project. Then only the source code remains to communicate the author's intentions.

Good style will also help you develop your code more efficiently and minimize bugs. A programmer's personal style develops over their career. It is to your advantage to take the time now to establish good programming practices. Your pesonal style must fit within a number of conventions followed by the industry. In CS302, we'll concentrate on the following aspects of style:

Braces {}

Recommendation: Always use braces even when they are not required. Selection and looping statements do not require braces if their body contains only one statement. A defensive programming practice is to use braces anyway. This helps avoid inadvertent errors while developing your code. However examples we provide and those in the text will sometimes leave out unnecessary braces.

Requirement: Line up braces. There are several common styles. Pick one style and use it consistently throughout your source files.

 Style Example
1
statement header 
{
    body
    body
}
This style is recommended and used by our text.
2
statement header {
    body
    body
}
This style is acceptable and used by many programmers.
3
statement header
    {
    body
    body
    }
This style is discouraged, but it is sometimes used by others.

Whitespace

Whitespace is essential for making source files readable. Meaningful parts of code are grouped together by using the whitespace as a separator. Whitespace is composed of horizontal whitespace (i.e. space and tab characters) and vertical whitespace (i.e. newline/return character makes blank lines).

Requirement: Use vertical whitespace to organize your source code into meaningful parts. For example, use blank lines to separate methods from each other, data members from methods, and import statements from comments. Blank lines are also used to separate groups of statements from eachother to make the major steps of an algorithm distinguishable in a method's body.

Requirement: Use tabs/spaces to indicate the level of nesting (see indentation).

Requirement: Use horizontal whitespace to organize each line of code into meaningful parts. There are many personal styles of spacing within a line. It is bad style to not use spaces within a line. For example:

good style result = 5 * Math.abs(xCoord - ++yCoord) / (11 % -time)
bad style! result=5*Math.abs(xCoord-++yCoord)/(11%-time)
good style for (day = 11; day < 22 && !done; ++day) {
ok style for (day=11; day<22 && !done; ++day) {
bad style! for(day=11;day<2&&!done;++day){

Indentation

Requirement: Lines should be of reasonable length. 80 columns including whitespace is typical. Going a little longer is okay but excessive line length is not acceptable.

Requirement: Use tabs/spaces to indicate the level of nesting. Indentation occurs in fixed-width intervals. Today most source code editors automatically indent for you typically using an interval of four spaces. It doesn't matter how much you indent as long as you are consistent throughout your source files. See the example, which uses four spaces per indentation.

Level 0: The following items are not indented:

Level 1: The following items are indented once:

Level 2+: Other statements are indented at least twice, which is the level of a method's body. Beyond level 2 you should indent once each time a statement is nested inside the body of another statement (see examples below). Always indent whether or not the control structure uses braces (see braces).

Statement Indentation Example
if & for loop
if (day == 31) 
{
    monthTotal = 0;
    for (int week = 0; week < 4; ++week) 
    {
        monthTotal += receipts[week];
    }
}
Indent each time a statement is put in the body of another statement.
chained if-elses
if (month >= 1 && month <= 3)
    quarter = 1;
else if (month >= 4 && month <= 6)
    quarter = 2;
else if (month >= 7 && month <= 9)
    quarter = 3;
else
    quarter = 4;
Combine else with nested if; indent if and else clause bodies to one level only.
switch form 1
switch (month) 
{
    case 2:
        daysInMonth = 28;
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        daysInMonth = 28;
        break;
    default:
        daysInMonth = 31;
        break;
}
An alternative is not to indent the case and default clauses.
switch form 2
switch (month) 
{
    case 2:  daysInMonth = 28;
             break;
    case 4:
    case 6:
    case 9:
    case 11: daysInMonth = 28;
             break;
    default: daysInMonth = 31;
             break;
}
This is the form the text uses.

Proper Naming in Java

Requirement: Source files are named the same as their class and ending with the extension .java. The spelling and capitalization must match, e.g. class Example would be in a file named Example.java

Requirement: Java requires you to follow these naming RULES when choosing names:

Requirement: Follow these naming CONVENTIONS when choosing names:

Requirement: Use descriptive names. The name need not be long, but it must relate to the intended use. Short (i.e. 1 letter) names can be used for temporary variables and loop counters.

Use Symbolic Constants instead of magic numbers

Requirement: Define symbolic constants to avoid the need to use literal values like 7, 100, or 3.1415927 in calculations or comparisons.

Such values when found in code are often referred to as magic numbers as they do not have any meaning except if the programmer remembers what the number referred to. Unfortunately, a new programers, or your grader may not know or be able to figure out what such a number represents.  So, unless the number has only one meaning and everyone knows it, it is better to define a symbolic constant and use that name in your code.

Can I just describe the value in the comments? Describing the use of such values in comments is helpful, but not as useful as defining a named constant to assign the value to a recognizable name.

Examples:

/** Number of rows and columns in the game board */
public static final int BOARD_SIZE = 8;

/** Stores the maximum number of players */
public static final int MAX_PLAYERS = 4;

/** Stores the id number of this object */
public final int ID;

Note: instance (non static) fields in instantiable classes may be declared as constants (final) and those constants may be assigned values via the constructor.

Requirement: Follow naming CONVENTIONS for defining symbolic constants, specifically:

Requirement: Use descriptive names. The name need not be long, but it must relate to the intended use. Short (i.e. 1 letter) names can be used for temporary variables and loop counters.