naming methods and variables : Java Glossary
- naming methods and variables
Literate naming is the #1 tool for writing maintainable code. Use the global rename
feature of your IDE (Integrated Development Environment)
exhaustively to continuously improve your names. You might find some of the
suggestions for terrible names in the How To Write Unmaintainable Code
section of the Java & Internet Glossary instructive. Keep in mind the
Unmaintainable Code essay in tongue in cheek. What follows is not.
Names of identifiers for classes,
methods and names can in theory be arbitrarily long. They are limited only by the
32-bit 2 gigabyte addressing limit of Strings. In practice
though, non-local names often end up inside class file which has its own limits which
tend to based around 16-bit addressibility. The only
restriction is they must start with a Java letter, upper or
lower case. That list of Java letters includes _ and
$, though $ should only be
used for mechanically generated code since JavaC uses $
in constructing names for anonymous classes. If you are unsure what constitutes a
Java letter use, Character. isJavaIdentifierStart( int ). The
category casts a broad net and includes all kinds of foreign alphabets. The rest of
the identifier can also contain the digits 0-9.
Newbies often violate these conventions because they imagine they are optional.
Granted javac.exe does not issue error
messages when you violate them, but your code will be unreadable to experienced Java
programmers if you do.
- Method names start with a lower case letter, followed by camel-case, e.g.
- Variable names also start with a lower case letter, followed by camel-case,
- Class and interface names start with a capital letter and followed by
camel-case, e.g. BikeSalesman
- static final constants are all upper case, with
words separated by underscores, e.g. INITIAL_WATER_DEPTH
Meaningful Method Names
One of the most time-consuming and
often neglected coding tasks, is renaming methods and variables to give ever clearer
intentions. The idea is to suggest in the name exactly what the method does, no more,
no less. You also want to distinguish it easily from all similar (non-overloaded)
methods. You have to balance the pain you cause forcing client code to rename against
the potential for confusion during future maintenance. A tool like
IBM (International Business Machines) ’s VAJ (Visual Age for Java)
takes out most of the pain. Most of this renaming work goes on in the early stages of
code development before the first release of a class. You would be amazed how much
renaming a method or variable helps you understand your own code. It greatly aids the
composition process. It gives you better tools to think with.
If you can’t come up with a good name for a method, there is a good chance
your method is not sufficiently unified and you should repartition the work.
Similarly, if you have trouble remembering in which class a method lives, it means
- The class name is not sufficiently descriptive of what its methods do.
- The class’s purpose is not sufficiently coherent. In organisational
jargon, get clear on the class’s mission
statement and reorganise until every member of the class is in alignment
If you keep forgetting or misspelling the name of a class, method or variable,
change it to something more memorable. When you stop forgetting it, you will know you
have finally correctly named it. If you are working on a team, keep in mind changing
names will confuse others even when it helps you. In that context, you have to get
the names right the first time. Think of naming more as a major life decision, like
naming a child.
The two most important pieces of advice I could ever give to a novice programmer
- Never include code in a method that does something not implied by the name of
- Never leave out of a method functionality implied by its name. It may
delegate details to other methods of course.
Consider Alphabetic Order
Name classes, variables and methods
in such a way that in a blind alphabetical listing, related items will be close to
each other in the list. You may discover there is a more convenient way to group your
methods and variables than you first chose. Globally rename and use the rearranger
feature of your IDE to sort the declarations in alphabetical order.
If you use any variant of Hungarian
notation where you put type information in your name, make sure you maintain it. If
you change the type of the variable or method, you must
globally rename it. Experiences with inaccurate Hungarian notation cause
most maintenance programmers to detest detest all forms of Hungarian notation.
method and variable names start with a
lower case letter, followed by camel-Case, e.g. calculateHypotenuse The conventions
for single letter local variables date back to FØRTRAN days. IntelliJ
inspector enforces them.
local variable conventions
||i, j, k, m, n|
retrieve a value from an object instance or static
class. Getter methods begin with the lower-case letters get followed by the camel-case name of the internal associated
private, possibly virtual, variable, e.g. getWidth(). Getters that return a boolean begin with the lower-case letters is e.g. isVisible();
store a value in an object instance or static class. Setter methods begin with the
lower-case letters set followed by the camel-case name of
the internal associated private, possibly virtual,
variable, e.g. setWidth( 10 ). Setters that take a
boolean parameter also begin with the lower-case letters
set e.g. setVisible(
Interfaces are usually adjectives, e.g.
Classes that are related to each other should have names that are related e.g.
BaseShape, CircularShape and
Arrays and Sets should have
plural names, e. g.