Vowels don't cost $500: Pontificating on Java 7 variable naming conventions
By Cameron McKenzie and Sal Pece
Every variable you declare in Java must be associated with a type, and it must also be given a unique name. When naming your variables, put a little bit of thought into it and name them well. Well thought out variable names make Java programs both easier to read and easier to maintain. Good names can even make Java programming fun.
This isn't an episode of Wheel of Fortune...
When consulting, I often have to remind developers that writing a Java program isn’t an episode of Wheel of Fortune – you don’t have to spin for a letter, and you don’t have to pay $500 for a vowel. I often see garbled code, where variable names have vowels arbitrarily stripped out of them or they have last few letters of a word curiously removed. Smaller variable names don’t translate into better code, and the length of a variable name doesn’t have any impact on runtime performance – it only impacts readability and maintainability, and long-term maintainability is the most important aspect of any computer program.
When naming a variable in Java, the variable name should always startWith a lowerCaseLetter while the rest of the name uses camelcase capitalization, which means thatForEverySubsequentWord in the variableName, theFirstLetterOfThatWord shouldBeUpperCased. The same rules go for choosing methodNamesAsWell. Java is case-sensitive, so a variable must be cased the same way every time it is used. Furthermore, two variables with the same name but different casings are treated as two separate variables.
Camelcase is the convention
Of course, this use of camelcase is only a convention. A variable name that starts with an upper case letter will not stop a program from compiling. A program that has variable names beginning with upper case letters will run just as well as a program written with variable names leading with lower case letters. However, from a professional standpoint, variable names that start with an upper case letter are completely unacceptable. If you create variables with leading upper case letters, you’ve just labeled yourself as an inexperienced amateur to the rest of the team.
double myNewIncome = myIncome + myIncome; // easy to read double i2 = i1 + i1; // difficult to read /* Dropping vowels or abbreviating important words does not make for good variable names. Compare the readability of the above two lines of code, and choose which application you’d rather be tasked with maintaining. */
In Java, the casing of your code conveys an incredible amount of information. An experienced programmer can gain an instant understanding of a Java source file based on how various words are cased, and to a lesser extent, spelled. Methods and variables should always start with a leading lower case letter, while the name of a class should always start with an upper case letter. Remember though, these are just conventions, and if you violate them, the compiler won’t complain. The compiler only flags compile errors – it doesn’t complain to you about poorly written code.
26 Jan 2012