or, write MyUrlClass, not MyURLClass
General Rules:
- Each word is capitalized
- no spaces
- only alphanumeric characters
A scheme in which the first word is capitalized, e.g. ThisIsUpperCamelCase, e.g. what's conventionally used in Java class names.
With an exception to the "each word is capitalized" rule, the difference is that the first word is not capitalized, e.g. thisIsLowerCamelCase, e.g. what's conventionally used in Java local variable names.
Example sentence 1:
Aaron is my name.
- Why is the name
Aaroncapitalized?
Example sentence 2:
My name is Aaron.
- Why is the word
Mycapitalized?- another rule of (again, not only) English sentences: the first word of a sentence is always capitalized.
- Why is the name
Aaroncapitalized?- see above^
- Convert sentence 1 to upper camel:
AaronIsMyName - Convert sentence 2 to upper camel:
MyNameIsAaron - Convert sentence 1 to lower camel:
aaronIsMyName - Convert sentence 2 to lower camel:
myNameIsAaron
These conversions are unremarkable, but there is one thing to note: in conversion #3, aaron is not capitalized. Why? Because the rule for lower camel case is that the first word is not capitalized. Keeping the original capitalization (e.g. boolean AaronIsMyName = true) would be wrong in Java, so we ignore the original capitalization and follow the camel case rule. Camel case doesn't care about your proper nouns or sentence rules. In other words:
Camel case does not preserve or respect the original case of the input.
Example title 1 (as in book/movie title):
Big Action and the Exciting Adventure
- Why are all words capitalized except
andandthe?- (some) rules of title case as used in names of books/movies/works of art/etc:
- all words are capitalized
- exceptions: conjunctions (and/or/...), prepositions (in/of/...), articles (the/a) are generally not capitalized
- exceptions to this^ exception: the first and last word are always capitalized
- (some) rules of title case as used in names of books/movies/works of art/etc:
- Convert title 1 to upper camel:
BigActionAndTheExcitingAdventure - Convert title 1 to lower camel:
bigActionAndTheExcitingAdventure
Again, unremarkable. Why is the word big in conversion #6 lowercase? After all, in title case, the first word is always capitalized. However the output isn't title case; it's camel case, and so we follow the rule of lower camel case and the first word is not capitalized, regardless of how it is in the input. And why are the words And and The capitalized in both? Same reason- in both upper and lower camel case, the first letter of every word (after the first) is capitalized, regardless of the original. Camel case doesn't care about your titles. Again:
Example fragment:
PhD students
- Convert fragment to upper camel:
PhdStudents - Convert fragment to lower camel:
phdStudents
For conversion #7, the input starts withPhD and the upper camel result starts with Phd, so it's not PhDStudents because according to the camel rules that each word has the first letter capitalized, that would mean the input tokens were Ph, D, and students. Why not make it PhDStudents? Well... for conversion #8, the lower camel result starts with phd. Arguably, if you wanted the upper camel output to be PhDStudents, then for consistency, you'd have to make the lower camel version phDStudents, which serves no useful purpose capitalizing only the 'D' in PhD. The conclusion: treat each token as a word, ignoring any capitalization rules of the input.
Some like to maintain all-caps acronyms when putting them in camel case.
- java.net.HttpURLConnection
- javax.xml.ws.http.HTTPException
- java.net.JarURLConnection
- org.omg.PortableInterceptor.ORBIdHelper
- javax.xml.bind.annotation.XmlIDREF
- XMLHttpRequest
- HTMLHRElement
I listed these examples because they clearly point out one of the problems with this approach; if you have two or more adjacent acronyms, the all-caps-maintaining pattern falls apart. One option is to maintain all-caps for one acronym and camel-case the other, as in HttpURLConnection above. URL maintains all caps, but HTTP is treated as a word.
In scheme 1 examples, some maintain all-caps for the first acronym in a series, some the second. There's no consistency to this attempted rule.
There's similarly no consistency about maintaining all-caps for specific "special" acronyms. Sometimes HTTP is all-caps above, sometimes not. The choice of which to maintain is arbitrary.
- java.sql.SQLXML
- javax.xml.crypto.dom.DOMURIReference
- javax.management.remote.rmi.RMIIIOPServerImpl
- JSONAPISerializer
The supposed benefit behind maintaining all-caps is that context of an acronym being an acronym is not lost; you know that a string of all-caps chars is an acronym. Even this fails when a name has multiple acronyms in either of these two schemes. In scheme 1, context of an acronym being an acronym is lost because the rule of maintaining all caps is not always followed (e.g. you can't discern that Jar was the acronym JAR by looking at JarURLConnection). In scheme 2, context of an acronym being an acronym is not lost, but context of where acronyms start and stop is lost, as all adjacent acronyms become mashed together.
Another big downside of maintaining all-caps: It requires human context to parse. With scheme 1, human knowledge of what is or isn't an acronym is needed to know whether a camel case token is a regular word or an acronym that is an exception to the scheme. With scheme 2, human knowledge of specific acronyms is needed to know where to split contiguous acronyms.
With either of the above 2 schemes, there's another problem. Imagine two Java classes ShortURL and URLAttributes, here we create variables with the same names as the classes.
ShortURL shortURL;
Above, standard Java naming convention, using all-caps acronyms, not a huge problem. But what if the class starts with an acronym:
URLAttributes urlAttributes;
URLAttributes uRLAttributes;
You can't maintain the all-caps acronym when using lower camel case. They're incompatible. The second option, uRLAttributes tries, but is misleading because now it looks like RL is the acronym.
The preferable solution is to treat acronyms and abbreviations as words, only capitalizing their first letters. The downside of this: you lose context by camel-casing all-caps acronyms, however that's not important. You lose context of all capitalization of all words when using camel case, whether acronyms, abbreviations, proper nouns or anything else. Camel case is inherently destructive to the input case, so there's no point in trying to maintain it.
// end of rant
See also:
Thank you.