Acronyms in Camel Back

一笑奈何 提交于 2019-11-29 22:53:20

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?

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.

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.

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
    

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!