Next: Size and complexity Up: Some advice on style Previous: Layout and indentation

Choosing identifiers for variables, methods, etc

One straightforward way to make a program more readable is to choose appropriate identifiers for classes, methods, variables, parameters, constants. The identifiers that you choose should have mnemonic value, that is, that they should remind the reader what the role or meaning of the identified object is; for example noOfCars. It is OK to use sensible abbreviations (e.g. max, tot).

This rule is more flexible when it comes to choosing identifiers for variables for certain purposes: for example, a for loop control variable where the actual value doesn't have any significance, or a temporary value during a calculation. However, even in these cases, there are common conventions, some arising from traditional mathematical practice, and some just through commonsense: integer variables, especially for array indexing, are i, i1, i2..., j, j1..., real variables are x, x1, x2..., y..., temporaries are t....

There is a Java convention that variable, parameter and method names start with a lower case letter, and that class names start with an upper class letter. The remainder of the identifier consists of upper and lower case letters and digits. The names of constants are often all in upper case, but it is an ill-defined convention.

Sometimes it is appropriate to have identifiers consisting of multiple words joined together, for example noOfCars. In Java it is conventional to start each word after the first with an upper case letter.


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