Next: Choosing identifiers Up: Some advice on style Previous: On arbitrariness and

Layout and indentation

In "Java for Students", Bell & Parr have adopted a convention on laying out Java programs on the page/screen which is typical of a style which most programmers in the world follow. It is described in Chapter 22. This will be our "house style".

First, let us consider the constraints imposed by reading a program on a screen or printed page - each of which shows only a limited amount of text at one time. Whichever way we are reading the text, we can call the visible area a "page". Sensibly sized "reading chunks" should be entirely visible on one "page", without scrolling or paper turning. This enables scanning by simple eye movement. Small classes might fit onto a "page", but probably typical classes will not fit on a "page". It is desirable that, when printed on paper, each class starts at the top of a sheet, but this can be awkward to arrange without software assistance. A class header and its instance variables might well fit on a "page". Methods should definitely fit on a "page". A method that overflows a single "page" is certainly too big. Its contents should be split into meaningful methods (but not too small!). We can consider grouping related items to avoid "page" turning. For example:

These improve what is often called the "locality"program.

The basic features of the indentation scheme make the structure of the program very clear to the (practised) eye. They include:

Study the examples of programs throughout Bell & Parr.

Erratic indentation is necessarily inconsistent, and is considered bad style.

Blank lines in between method declarations and in between major sections of code within a method are also helpful. But do not put a blank line between every line of the program.



Next: Reserved words and Up: Some advice on style Previous: On arbitrariness and


Email: Dr Simon B Jones sbj@cs.stir.ac.uk