Conventions for naming parameters
1. Use short, easily remembered or mnemonic names. You can do this because:
(a) Parameters are explained in the method specification.
(b) Method bodies are short enough to allow easy access to the parameter
specs. (Many methods are short enough to fit on a computer screen.)
2. Remember that methods are usually written to be general, and parameter
names should reflect that generality. For example, consider a function that finds
the minimum of two variables price and salePrice . Do not name the parame-
ters price and salePrice , name them x and y , and write a general function to
find the minimum of x and y .
Conventions for naming local variables
1. Use short names for local variables, making them mnemonic if possible, but
be sure to explain the use of each local variable in a comment that is placed near
its declaration. You can use short names because:
(a) Next to the declaration of each local variable is its definition.
(b) Method bodies are short enough to allow easy access to the declaration
of and comments for local variables. (A method is generally short enough
to fit on a computer screen.)
2. A local variable that is used in just a few statements may not need a comment.
Conventions for naming instance variables and class variables
1. Use longer names that convey some idea of the meaning of instance variables
and class variables. You should do this because their uses are often far removed
from the comments describing them. Using longer names may help reduce the
need to look at the definitions, thus saving the reader some time (e.g. priceOf-
Pie ). But be sure that defining comments appear near their declaration.
2. Do not use hugely long names because the longer the names, the longer and
more complicated the program appears to be. Do not try to encapsulate the whole
definition of an instance or class variable in its name.
Convention for naming constants (vars with modifiers static and final )
1. Use all capital letters, and use the underline character " _ " to separate words
within a name (e.g. NUMBER_OF_SIDES ).