36

I often see Java class names like

XmlReader

instead of

XMLReader

My gut feeling is to completely upper case acronyms, but apparently many people think differently. Or maybe it's just because a lot of code generators are having trouble with acronyms...

So i would like to hear the the public opinion. How do you capitalize your class names containing acronyms?

Benno Richters
  • 15,378
  • 14
  • 42
  • 45
Stroboskop
  • 4,327
  • 5
  • 35
  • 52
  • possible duplicate of [Camel case names: How strict?](http://stackoverflow.com/questions/486870/camel-case-names-how-strict) – Daniel Fischer May 27 '12 at 12:59

6 Answers6

52

We use the camel case convention like Java and .NET do. Not for reasons of code generators, but for readability. Consider the case of combining two acronyms in one name, for example a class that converts XML into HTML.

XMLHTMLConverter

or

XmlHtmlConverter

Which one do you prefer?

AronVanAmmers
  • 1,668
  • 20
  • 24
  • 12
    This is the recommended approach in Effective Java as well. – Jack Leow Jul 24 '09 at 11:06
  • 8
    Ok, bringing up Bloch clinches it. – Stroboskop Jul 31 '09 at 10:58
  • 2
    Why not XML2HTMLConverter?!?!?!?!?!? – Roberto Lo Giacco Mar 01 '12 at 18:09
  • 1
    @RobertoLoGiacco, 2 is not a word. And that technique would limit the class to only be able to convert XML to HTML. Why not just have one XmlHtmlConverter class that goes both ways? I'd use HtmlXmlConverter though (alphabetical order) – rickmzp Mar 02 '12 at 05:31
  • @digx, I don't see why you should limit class names to alphabetic characters only, anyway I guess it's again a matter of taste and habits: I see much more easy to read XMLReader than XmlReader as I'm used to write/read acronyms in uppercase format (like most developers I guess). Regarding the "directional" nature of the class name it can open another topic: why should you use the same converter for two way conversions? I'm not going to open that discussion though! – Roberto Lo Giacco Mar 02 '12 at 12:59
  • So what happens when the acronym can sorta be read as a word? Like ROIProdictionService or RoiPredictionService. With the latter case, you wouldn't expect that Roi is actually an acronym. – shoebox639 Sep 06 '13 at 20:13
  • @shoebox639: maybe write it in full, ReturnOnInvestmentPredictionService? Otherwise my preference would be RoiPreductionService. The fact that it can sort of be read as a word in my opinion is even more of an argument to treat it like any other word (start with an uppercase letter, the rest in lowercase). – AronVanAmmers Oct 09 '13 at 12:24
  • Agreed @AlexanderOrlov, however it's not camel case / pascal case, where every word should start with a capital. For example I find ```xml-to-html-converter``` just as readable too, but it uses a different naming convention. – AronVanAmmers Feb 13 '15 at 11:42
  • 1
    I would prefer `XMLToHTMLConverter`. Fully camel-cased, still readable, and represents what it does even more clearly. Noun-only names usually are not good even at English level. – eonil Apr 03 '15 at 23:26
  • It's usu. better to mirror natural language in both IT Ids and structure of the code (relative to the specs assuming it wouldn't result in significant dupe code / performance issues), but for IT Ids (obviously other than those where space/dash/underscore can and are typically used to separate words / acronyms), your choice to avoid the "significant cost" of using space/dash /underscore means you must also "pay the price" by proper-casing acronyms, and it's better to do so consistently (even if they're separated by a lower case word/digit, "only" 2 chars, "well known", other "excuse", etc.). – Tom Oct 29 '18 at 20:32
16

Two reasons:

  1. It's easier to distinguish where one acronym ends and the other begins in identifiers where they're placed after each other, for instance in XmlHtmlConverter. Now XML and HTML aren't such good examples, because everyone knows what XML is, and what HTML is. But sometimes you'll see less obvious acronyms and then this becomes important.
  2. Eclipse is smart with words and their initials. For XmlHtmlConverter, you can type in XHC in the Open Type dialog and it will find it. For an XMLHTMLConverter, the initials would be XMLHTMLC which is of course a bit longer.
jqno
  • 15,133
  • 7
  • 57
  • 84
3

I find that XMLReader is more difficult to read. The reason is that you can't easily tell where the words are separated. I believe that acronyms with lower case should be accepted. You may be able to use upper case for class definitions, but what about instance variables:

XmlReader xmlReader;

Here you have to use lower case anyhow.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • 1
    It would still be `XMLReader xmlReader = new XMLReader();` XML counts as the first word, so you decapitalize the whole thing. – NobleUplift Oct 18 '13 at 15:43
3

For acronyms I use the following rule :

  • If the acronym is of length 2, put the acronym in upper case.

      For Ex : UIRule
    
  • If the acronym is of more length, I use the pascal casing for the acronym

      For Ex : SmsValidation, XmlReader
    
Jey Geethan
  • 2,235
  • 5
  • 33
  • 60
2

Pascal Case is used in the .NET framework. So

XmlReader

is preferred in Microsoft environments.

I have to agree with AronVanAmmers that this is easier to read that the alternative.

Reference: Microsoft Design Guidelines for Class Library Developers

Thomas Bratt
  • 48,038
  • 36
  • 121
  • 139
0

I believe that, in general, class names should be written as you expect them to be read. For example, when speaking the class name aloud I would say "X-M-L reader", so I would name the class "XMLReader". However, I would name a hypothetical "REST service" class "RestService", since, in general, "REST" is not pronounced "R-E-S-T" but "rest". For something like "SQL", I could go either way, since some people say "S-Q-L" and others say "sequel". But it really just comes down to personal preference.

Greg Brown
  • 3,168
  • 1
  • 27
  • 37