views:

357

answers:

5

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?

+12  A: 

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
This is the recommended approach in Effective Java as well.
Jack Leow
Ok, bringing up Bloch clinches it.
Stroboskop
+1  A: 

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
+1 `XMLReader xMLReader = new XMLReader("wtf!?");`
Jørn Schou-Rode
+2  A: 

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
    
Xinxua
+4  A: 

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
+1  A: 

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