Objects First With Java - A Practical Introduction Using BlueJ
Program Style Guide
Version UW Cosc 1010

1. Naming

1.1 Use meaningful names.

Use descriptive names for all identifiers (names of classes, variables and methods). Avoid ambiguity. Avoid abbreviations. Simple mutator methods should be named setSomething(...). Simple accessor methods should be named getSomething(...). Accessor methods with boolean return values are often called isSomething(...), for example, isEmpty().

1.2 Class names start with a capital letter.

1.3 Class names are singular nouns.

1.4 Method and variable names start with lowercase letters.

All three - class, method and variable names - use capital letters in the middle to increase readability of compound identifiers, e.g. numberOfItems.

1.5 Constants are written in UPPERCASE.

Constants occasionally use underscores to indicate compound identifiers: MAXIMUM_SIZE

2. Layout

2.1 One level of indentation is four spaces.

2.2 All statements within a block are indented one level.

2.3 Braces for classes, methods and all other blocks are alone on one line.

The braces for classes, methods, and all other blocks are on separate lines and are at the same indentation level, for example:

public int getAge()
{
      statements
}

2.5 Always use braces in control structures.

Braces are used in if-statements and loops even if the body is only a single statement.

2.7 Use a space around operators.

2.8 Use a blank line between methods (and constructors).

Use blank lines to separate logical blocks of code. This means at least between methods, but also between logical parts within a method. Do not use a blank line between a method and its comment.

3. Documentation

3.1 Every class has a class comment at the top.

The class comment contains at least
- a general description of the class
- the author's name(s)
- a version number

Every person who has contributed to the class has to be named as an author or has to be otherwise appropriately credited.

A version number can be a simple number, a date, or other formats. The important thing is that a reader must be able to recognise if two version are not the same, and be able to determine which one is newer.

3.2 Every method has a method comment.

3.3 Class and method comments are Javadoc-readable.

Class and method comments must be recognised by Javadoc. In other words: they should start with the comment symbol /**.

3.4 Code comments (only) where necessary.

Comments in the code should be included where the code is not obvious or is difficult to understand (while preference should be given to make the code obvious or easy to understand where possible), and where it helps understanding of a method. Do not comment obvious statements - assume your reader understands Java!

4. Language use restrictions

4.1 Order of declarations: fields, constructors, methods.

The elements of a class definition appear (if present) in the following order: package statement; import statements; class comment; class header; field definitions; constructors; methods.

4.2 Fields may not be public (except for final fields).

4.3 Always use an access modifier.

Specify all fields and methods as either private, public, or protected. Never use default (package private) access.

4.4 Import classes separately.

Import statements explicitly naming every class are preferred over importing whole packages. E.g.

import java.util.ArrayList;
import java.util.HashSet;

is better than

import java.util.*;

4.5 Always include a constructor.

4.6 Initialise all fields in the constructor.



Copyright notice

Copyright (c) Michael Kölling and David Barnes.

This style guide was written for the book Objects First With Java - A Practical Introduction Using BlueJ.

Permission is granted to everyone to copy, modify and distribute this document or derived documents in any way. We would appreciate a reference to the original source in derived documents.