Java Naming Convention with Acronyms

JavaNaming Conventions

Java Problem Overview


What is the correct name for the following Java class: DVDPlayer or DvdPlayer?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

Some examples from the JavaSE classes, apache commons and spring:

  • HttpURLConnection
  • HTTPAddress
  • UrlPathHelper
  • AopProxy
  • ISBNValidator

So - it doesn't really matter.

Solution 6 - Java

Effective Java seems to prefer DvdPlayer.

Solution 7 - Java

As others have indicated, its a style thing that differs across projects. Google projects such as Guava and GWT prefer the DvdPlayer style.

https://google.github.io/styleguide/javaguide.html#s5.3-camel-case

Solution 8 - Java

From sun java docs:

>Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

Solution 9 - Java

DVDPlayer is the standard, but DvdPlayer is not uncommon.

You more often than not see getId. That's probably due to thinking ID is a shortening of "Identity". It is actually the initials of Identity Document.

HttpURLConnection is often given as an example of mixed convention. However, "http" used as protocol name in a URL should be lower case (although upper case is often accepted).

Solution 10 - Java

There is no "correct", only preferences here.

Sun is consistent in the way they name classes containing "URL" and "HTML", but I see HTTP using both all caps and camel case in the javadocs.

Personally, I'd prefer DvdPlayer.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDD.View Question on Stackoverflow
Solution 1 - JavaKevin CathcartView Answer on Stackoverflow
Solution 2 - JavaflybywireView Answer on Stackoverflow
Solution 3 - JavaJaakkoKView Answer on Stackoverflow
Solution 4 - JavaPeter PerháčView Answer on Stackoverflow
Solution 5 - JavaBozhoView Answer on Stackoverflow
Solution 6 - JavaBrian HarrisView Answer on Stackoverflow
Solution 7 - JavaSteven BenitezView Answer on Stackoverflow
Solution 8 - JavacodaddictView Answer on Stackoverflow
Solution 9 - JavaTom Hawtin - tacklineView Answer on Stackoverflow
Solution 10 - JavaduffymoView Answer on Stackoverflow