问题
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?
回答1:
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?
回答2:
Two reasons:
- 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. - Eclipse is smart with words and their initials. For
XmlHtmlConverter
, you can type inXHC
in the Open Type dialog and it will find it. For anXMLHTMLConverter
, the initials would beXMLHTMLC
which is of course a bit longer.
回答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.
回答4:
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
回答5:
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
回答6:
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.
来源:https://stackoverflow.com/questions/1176950/acronyms-in-camel-back