Guidelines on Programming Style and Documentation

Programming style deals with the appearance of your program. If you were to write an entire program on one line, it would compile and run properly, but doing this would be bad programming style because the program would be hard to read. Documentation consists of explanatory remarks and comments for the program. Programming style and documentation are as important as coding. Good programming style and appropriate documentation reduce the chance of errors and make programs easy to read. First, here is a sample code:

/**
 * Class: CSCI3301-03 Software Engineering
 * Instructor: Y. Daniel Liang
 * Description: (Give a brief description for Exercise 1)
 * Due: 1/18/2001
 * @author John F. Smith
 * @version 1.0
 */
public class Exercise1 {
  /**Main method*/
  public static void main(String[] args){
    double radius;
    double area;

    // Prompt the user to enter radius
    System.out.print("Enter radius: ");
    radius = MyInput.readDouble();

    // Compute area
    area = radius*radius*3.14159;

    // Display results
    System.out.println("The area for the circle of radius " +
      radius + " is " + area);
  }
}

Appropriate Comments and Comment Styles

Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. In a long program, you should also include comments to introduce each major step and to explain anything that is difficult to read. It is important to make your comments concise so that you do not crowd the program or make it difficult to read.

Use the javadoc comments (/** ... */) for commenting an entire class and an entire method. These comments must precede just before the class or the method header and they can be extracted in a javadoc HTML file. For commenting the steps inside a method, use line comments (//). For information on javadoc comments, see http://java.sun.com/j2se/javadoc/. 

JB4 TIP: Use Ctrl+/ as a line comment toggle. Ctrl+/ adds comment tags (//) to the line of code the cursor is on or removes existing comment tags. Highlight blocks of code and use Ctrl+/ to comment or remove existing comments on an entire block.

Naming Conventions

Make sure that the meanings of the descriptive names you choose for variables, constants, classes, and methods are straightforward. Names are case-sensitive. Listed below are the conventions for naming variables, methods, and classes.

·       For variables and methods, always use lowercase. If the name consists of several words, concatenate them into one, making the first word lowercase and capitalizing the first letter of each subsequent word in the name; for example, the variables radius and area and the method readDouble.

·       For class names, capitalize the first letter of each word in the name; for example, the class name ComputeArea.

·       All letters in constants should be capitalized, and underscores should be used between words; for example, the constant PI and constant MAX_VALUE.

TIP: It is important to become familiar with the naming conventions. Understanding them will help you to understand Java programs. If you stick with the naming conventions, other programmers will be more willing to accept your program.

TIP: Do not choose class names that are already used in the Java standard packages. For example, since the Math class is defined in Java, you should not name your class Math.

Proper Indentation and Spacing

A consistent indentation style makes programs clear and easy to read. Indentation is used to illustrate structural relationships among the program’s components or statements. Java can read the program even if all of the statements are in a straight line, but it is easier to read and maintain code that is aligned properly. You should indent each subcomponent or statement two spaces more than the structure within which it is nested.

Spacing lines should be used to separate segments of the code to make the program easier to read.

Block Styles

A block is a group of statements surrounded by braces. A block can be written in many ways. For example, the following are equivalent:

public class Test
{
  public static void main(String[] args)
  {
    System.out.println("Block Styles");
  }
}
 
public class Test {
  public static void main(String[] args) {
    System.out.println("Block Styles");
  }
}

The former is referred to as the next-line style, and the latter, as the end-of-line style. This book uses the end-of-line style.

Summary