Java: Names
Names should be meaningful and follow conventions
Source code is read by the compiler, which doesn't care about your writing style, and people, who care enormously about the readability of your code. To be readable, the names should be meaningful and they should follow the Java conventions for capitalization and use of the underscore.
Meaningful names
Variables, methods, classes, etc must be given names that mean something to the reader (temperature, velocity, ...). Avoid names like a, b, and c unless they are truly without special meaning.
I worked with someone who claimed he couldn't think of meaningful variable names, so used v1, v2, v3, ... He was quite smart, but his programs were a nightmare to read. Fortunately, he soon moved into management.
Sadly, I must admit that some
of my own old programs have mysterious names like
nbrt
, which must have been an abbreviation for something.
Using abbreviations isn't necessarily bad, but they must either be commonly
understood or documented in comments where the variable is declared.
Single use. Do not use a variable for more than one purpose. Local variables are very cheap so you should always declare a new variable, rather than reuse one for more than one purpose.
Language rules
Identifiers, which is what names are usually called in programming languages, must obey the rules of the particular programming language you are using.
Java identifiers start with a letter or underscore and may be followed by zero or more lower case or upper case letters, decimal digits, or underscore characters. Technically the dollar sign is also allowed, but should never be used because it may conflict with compiler-generated internal identifiers.
Conventions
In addition to being legal, programmers expect identifiers to follow standard conventions. These conventions often vary somewhat from language to language. The standard Java conventions are explained below. Your organization (or instructor) may have additional naming conventions.
Names consisting of multiple words
Because blanks can't be used in identifiers, multi-word names are handled by using camel case in Java, or separating the words with underscores in some other languages (eg Ruby).
Camel case marks the start of a new word by writing the first letter in upper case.
double dailyAverage; int maximumNumberOfSunnyDays;
A popular alternative to camel case in some languages is to separate the words with an underscore. Altho underscores are a good convention, they won't be accepted in Java programs, with one exception as noted below.
Sun's conventions for Java identifiers
Use the following conventions, which are almost universally followed in Java programs.
- Class names (and interface names) start with a upper case letter and are continued in
camel case.
Examples:String
,JOptionPane
,DecimalFormat
. - Variable and method names begin with a lower case letter and are continued in
camel case.
Examples:dailyAverage
,maximumNumberOfSunnyDays
- Constant names should be entirely in upper case, with multiple words
separated by underscores.
Examples:EXIT_ON_CLOSE
,MILES_PER_KILOMETER
.
Review questions
The following are all accepted by the compiler without error, but they all should be changed. How?
public class celsiustofahrenheit {
int maximum_temperature;
static double MilesPerKilometer = 0.6;
private double compute(double n) {return n * 0.000144;}
References
- Sun's Java coding standards can be found at java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
- Hungarian notation should probably be avoided. See See www.joelonsoftware.com/articles/Wrong.html .