naming methods and variables : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

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.
Technical Requirements Getters
Conventions Setters
Meaningful Method Names Interfaces
Consider Alphabetic Order Plurals
Hungarian Notation Links
Local Variables

Technical Requirements

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.

Conventions

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.

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 either:

The two most important pieces of advice I could ever give to a novice programmer are:
  1. Never include code in a method that does something not implied by the name of the method.
  2. Never leave out of a method functionality implied by its name. It may delegate details to other methods of course.
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.

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.

Hungarian Notation

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.

Local Variables

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.
int i, j, k, m, n
float f
double d
byte b
char c, ch
long l
String s, str

Getters

Getter methods 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();

Setters

Setter methods 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( true );

Interfaces

Interfaces are usually adjectives, e.g. Serializable, Externalizable. Classes that are related to each other should have names that are related e.g. BaseShape, CircularShape and RectangularShape.

Plurals

Arrays and Sets should have plural names, e. g.

This page is posted
on the web at:

http://mindprod.com/jgloss/naming.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\naming.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[18.191.240.243]
You are visitor number