269

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

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
DD.
  • 21,498
  • 52
  • 157
  • 246
  • 130
    I hate acronyms. `DigitalVersatileDiscPlayer` is the way forward. – Tom Hawtin - tackline Feb 10 '10 at 12:52
  • 9
    +1 to Tom for the joke. I do find this question helpful if "correct" is reinterpreted as "standard" or "most typical". The accepted answer is great! – Jon Coombs Jun 20 '14 at 04:06
  • 7
    For me it makes sense to think of such acronyms as a single word and as such I follow the [convention](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html) and use `DvdPlayer`. – Daniel Jan 19 '15 at 17:02
  • 9
    [The style guide](http://cr.openjdk.java.net/~alundblad/styleguide/index-v6.html#toc-variables) has the following to say about it: *"Format an abbreviation as a word if the it is part of a longer class name."*, so `DvdPlayer` is the way to go. (And, for Tom, *"Use whole words and avoid using abbreviations unless the abbreviation is more widely used than the long form."*, and I think "DVD" is more widely used than "Digital Versatile Disc" :-) – aioobe Apr 04 '16 at 19:53
  • You certainly meant "Discus", didn't you? :) – Ville Oikarinen May 22 '19 at 11:56
  • For the acryonym haters: some are good, e.g. foreign language ones. Example: CFI = Condor Flüge Individuell -> `isCfiBooking()` vs. `isCondorFlügeIndividuell()` in an international team with the source code language being English? Nah. – Kawu Nov 10 '22 at 12:37

10 Answers10

300

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.

Kevin Cathcart
  • 9,838
  • 2
  • 36
  • 32
  • 1
    I like this, except that it's not consistent to have CAPS for 3- and 2-letter acronyms. (Maybe I'm to much of a purist, but see the accepted answer for practical reasons. Plus, there's the confusion factor.) – Jon Coombs Jun 20 '14 at 04:09
  • 1
    @JCoombs: Well, the inconsistancy for 2 letters is to something like `IpAddress` which looks terrible to many people. Personally when I need to write Java code, I go with mixed case for the 3 letter acronyms, leaving only the two letters as a special case. – Kevin Cathcart Jun 20 '14 at 17:07
  • 5
    What if your class name has multiple adjacent two-letter acronyms? Regardless of what you do, it'll look 'wrong' - USGFCharset, UsGfCharset, US_GFCharset... – Kevin Aug 08 '14 at 02:10
  • 1
    Then what about XMLDBOK? I'd better see it as XmlDbOk and always use camelCase – gaRex Jan 12 '16 at 06:52
  • 1
    @gaRex, The .Net guidelines say that that would be `XmlDbOk`. Db is considered an abbreviation rather than an acronym because database is one word. (I'll fix my answer). Abbreviations use PascalCase or camelcase. In java I would use probably use `XMLDbOk`, mainly for consistancy since XML is usually captialized in the core java frameworks. – Kevin Cathcart Jan 14 '16 at 17:36
  • 1
    > usually captialized in the core java frameworks People traditionally thought, that Sun is around Earth. Orthodoxality is better in religion )) – gaRex Jan 14 '16 at 21:05
  • 9
    Really good answer. But personally I don't like the .NET guidelines' idea of naming differently depending on length of acronyms and whether or not it is an abbreviation. Who actually cares about and checks the length of an acronym? Or if it is an abbreviation? I prefer a blanket rule for naming. The problem comes when using 3rd party libraries with different conventions from whatever rule you chose to go with. – Amani Kilumanga Feb 18 '16 at 01:09
  • based on answer I assume, `isValidDvd` will be preferred over `isValidDVD` – Akshay Vijay Jain Jul 13 '20 at 08:05
  • Seems few things are consistent in Java. For example, sentences in Javadocs don't always end in with periods or start with uppercase letters. Little things like that drives me nuts. You can at least follow well-known grammar rules. – Mike Lowery Dec 14 '22 at 23:25
113

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

Community
  • 1
  • 1
flybywire
  • 261,858
  • 191
  • 397
  • 503
59

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.

JaakkoK
  • 8,247
  • 2
  • 32
  • 50
  • 3
    It's also faster to type this way. – Ates Goral Apr 18 '11 at 14:58
  • 7
    I think "DVDPlayer" makes the word boundaries more clear. "Dvd" is not a word, whereas "DVD" is an acronym for the words "Digital Versatile Disc". So the actual word boundaries in "DVD Player" are at "D", "V", "D", and "P". – Greg Brown Oct 22 '12 at 12:37
  • 30
    @GregBrown: A DVD is a disc with a hole, nobody except you knows its full name, neither cares about it. And it is far more practical to use DvdPlayer. – Igor Rodriguez Jun 24 '14 at 10:54
  • @IgorRodriguez: Just because you like "DvdPlayer" better does not make it any more practical than "DVDPlayer". The point is that, when speaking, acronyms are read letter by letter, not pronounced like an actual word. In my opinion, class and method names should mirror this convention. – Greg Brown Dec 31 '14 at 19:54
  • 5
    @GregBrown: In fact it is more practical at least in Eclipse, where the camel-case recognition is a pain with acronyms: i.e. with DvdPlayer you can type "DP" and press Ctrl+1 to get the choice to select DvdPlayer, but if you had DVDPlayer you would have to type "DVDP". And is even more annoying if it is longer. I wouldn't like to have an UNESCOConnector in my code. Anyway it is a matter of choice. – Igor Rodriguez Jan 01 '15 at 08:51
  • 40
    Another good example is HTTPSID - did I mean HTTP SID or HTTPS ID... Therefore it should be written HttpSid or HttpsId respectively to better explain the meaning. – Oz Edri Feb 17 '16 at 08:33
  • What about acronym PET vs word Pet? If you use camelcase for acronyms e.g. PetService, it's not clear whether you mean PET or Pet. – onepiece Oct 04 '19 at 18:39
  • @onepiece - you'll run into this sort of thing anyways if you create an all-caps constant. is PET_ID referring to PET or pet? A reader should hopefully be able to figure it out based on context. – Scotty Jamison Apr 12 '22 at 04:16
45

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.

Peter Perháč
  • 20,434
  • 21
  • 120
  • 152
  • 18
    What's wrong with `DVDPlayer dvdPlayer;`? – azz Aug 15 '14 at 13:16
  • 14
    @DerFlatulator What's not wrong with it? No matter how you came to `dvdPlayer`, when you go back, you'll get `DvdPlayer`. – maaartinus Apr 23 '15 at 19:34
  • 7
    @DerFlatulator: it's a question of automation when converting from UpperCamelCase to lowerCamelCase: I had the problem when automating Hibernate mapping to a snake_case: `DvdPlayer -> dvd_player` but `DVDPlayer -> d_v_d_player`. There is no way to automate DVDPlayer to dvd_player. – pdem Jun 01 '16 at 09:48
  • 1
    @pdem `/^([A-Z]+)[A-Z]\w+$/` and `$1` to lower case? – azz Jun 01 '16 at 23:26
  • 3
    @DerFlatulator: okay,thanks for the reply, it worked in this case. But I'm still convinced with the "DvdPlayer" notation: what about DVDRPGPlayer?, it should be converted to dvdRpgPlayer, not dvdrpgPlayer. – pdem Jun 02 '16 at 08:19
  • 2
    Also consider your getters and setters: `getDvdPlayer()` works better than `getDVDPlayer()`, for example, when used with JSP EL (e.g., `foo.dvdPlayer`). And if you're naming your getters with lowercase in the acronyms, it's best to keep your class names the same for consistency and predictability. – daiscog Feb 22 '18 at 15:02
40

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

  • HttpURLConnection
  • HTTPAddress
  • UrlPathHelper
  • AopProxy
  • ISBNValidator

So - it doesn't really matter.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
30

Effective Java seems to prefer DvdPlayer.

Brian Harris
  • 2,735
  • 3
  • 22
  • 34
15

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

Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
  • 1
    A more general Google convention link for `DvdPlayer` style: https://google-styleguide.googlecode.com/svn/trunk/javaguide.html#s5.3-camel-case – Stan Kurdziel Apr 28 '15 at 00:14
  • 1
    The link in the answer has moved to http://www.gwtproject.org/makinggwtbetter.html#codestyle The link @StanKurdziel mentions has moved to https://google.github.io/styleguide/javaguide.html#s5.3-camel-case – Jeroen Wiert Pluimers Jun 05 '18 at 09:41
8

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).

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 19
    That doesnt really say anything about whether it should be upper or camel case. – DD. Feb 10 '10 at 14:29
  • @DD. I think to me, "much more widely used" points to use all-caps for relevant acronyms (HTTP, GET, etc) than for some random business-specific word like DVD, MRI, MAGA, etc. – goelakash Jun 03 '18 at 15:53
2

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).

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
0

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.

duffymo
  • 305,152
  • 44
  • 369
  • 561