Java Notes

Variable Names

Basic variable naming conventions

Choosing good names is probably the single most important factor in making programs readable -- or unreadable. It's worth spending some time to get good names. When I come back to a program of mine and find unhelpful names, the first refactoring I apply is to replace bad names with good names. Why would I choose bad names in the first place? When everything is fresh in your mind, you don't need the extra help of good names so its easy to choose an unhelpful name. Later, or to another reader, it's often obvious.

Sun's Code Conventions for the Java Programming Language (java.sun.com/docs/codeconv/) is the most widely used set of Java coding guidelines. Altho the organization you work for may have have different standards in some aspects, these variable naming conventions are almost always used. Here is what they have to say about variable names.

"Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

Variable names should be short yet meaningful. The choice of a variable name should be mnemonic - that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters."

Meaningful

Differentiating by Lifetime (Local, Instance, and Class variables)

Hints about about a variable's lifetime can be quite useful. Sun gives no guidance on this, and as a result, there are many different styles, including the most popular -- no naming distinction.

Differentiating type - Hungarian notation

Obfuscation