Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Naming Convention with Acronyms [closed]

Since it looks like the answer is that there is no single standard for this in Java, I'd like to note that the .NET Framework Design Guidelines do specify this.

Now before slamming me for being off topic, please remember that the class naming guidelines for Java and the .NET Framework are quite similar, which makes the .NET guidelines useful as a persuasive reference.

General Rules

Both guidelines recommend only using acronyms when the acronym is widely known and well understood. DVD or XML are excellent examples of this, as while you will recognize them immediately, it would take a bit longer to recognize the expanded version.

Abbreviations

The .NET Framework Guidelines recommend not to use abbreviations (as opposed to acronyms), except that two common abbreviations ID and OK may be used in identifiers. When using an abbreviation, mixed case Id is always used except for the first word of a camelCase identifier (as opposed to a PascalCase identifier).

In Java this convention is followed only some of the time. Take a look at how mixed the spellings getID and getId are in the JCL. (Scroll partway down that page). In the Java 8 version though, getId is used more and more, which hints the PascalCase convention is preferred nowadays. It is best to just avoid abbreviations entirely when possible.

Short Acronyms

The .NET Framework Guidelines say that two letter acronyms like IO, should have the same case for both letters. So for PascalCase identifiers (like a class name) you would get DBRate, while for a camelCase identifier (like a local variable) you might have ioChannel.

This definitely seems to be the prevailing convention in Java as well.

Long Acronyms

The .NET Framework guidelines recommend that acronyms three letters or longer use mixed case for PascalCase and camelCase identifiers, except for the first word of a camelCase identifier. Thus for a class name you might have XmlDocument, while a local variable might be named httpRequest.

This convention is not always followed in Java. Four character acronyms do seem to usually use mixed case, but even the JCL is not consistent about three letter acronyms. Most of them seem to be all uppercase, like URL, XML, SQL, and DOM, but there are some exceptions like Jar.

Conclusion

For Java:

For 4+ letter acronyms, use mixed case. The standard library does this, and it just makes good sense.

For 3 letter acronyms, you can use all uppercase like the JCL, or you can use mixed case like the .NET Framework does. Either way, be consistent.

For 2 letter acronyms, use all uppercase.

For 2 letter abbreviations, Java does not really have a standard, but I suggest using mixed case, unless consistency with other names would make all uppercase look better.


There is no "correct" answer. Just a set of practices and conventions that better play with your other tools.

Therefore I prefer DvdPlayer. It is more helpful as in Eclipse you can do Ctrl+Shift+T and pick classes by the first letter of each word.

alt text


I've seen both of them used in the wild, and Sun seems to go for the DVDPlayer style. I prefer DvdPlayer, though, because that way it is clear where the word boundaries are even if there are multiple consecutive acronyms, as in HTTPURLConnection.


I like to define individual instances of classes in the following fashion:

Catalogue catalogue;
Person person;

Therefore, if I used DVDPlayer, what would I call an instance of that? dVDPlayer? Hence I'd choose the DvdPlayer class name, so you can name the instances like dvdPlayer.