Stroboskop
Stroboskop

Reputation: 4356

Acronyms in Camel Back

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?

Upvotes: 36

Views: 8867

Answers (6)

Greg Brown
Greg Brown

Reputation: 3254

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.

Upvotes: 0

Thomas Bratt
Thomas Bratt

Reputation: 51812

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

Upvotes: 2

jqno
jqno

Reputation: 15520

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.

Upvotes: 16

Jey Geethan
Jey Geethan

Reputation: 2285

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
    

Upvotes: 3

kgiannakakis
kgiannakakis

Reputation: 104178

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.

Upvotes: 3

AronVanAmmers
AronVanAmmers

Reputation: 1666

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?

Upvotes: 52

Related Questions