I am having a problem in understanding how to apply camelCase syntax to some of my variable names.

For example, how should I correctly write a word like "phonenumber" in camel case? Is it phoneNumber or phonenumber? Similarly with "username", is it username or userName?

I think it doesn't look right with camel case like motorCycle, passWord, sunDay, setUp or waveLength since these are just one word each. I think that could be why it's called hashMap but also hashtable in camel case without the capital in the last case because hashtable is one word while hash map is two words.

But if the motorcycle has a color then would it be motorcycleColor since a word is concatenated? Is that correct or should it be phoneNUmber, waveLength, sunBlock and even sunDay for the Sunday of the week?

Why for instance is the method called getISOCountries while it says HttpHeaders e.g. it's not clear what becomes lowercase if we have a method like String camelCaseString = dog.toCamelCase() or interface CamelCase.

For cases where it's really hard for you to tell if a two-word phrase is a compound word, pick a capitalization and stick with it. As with most coding conventions, being consistent within a single project is what's most important.
–
KevinFeb 25 '14 at 3:21

2

Great question, I always asked myself how to write "fileName" (and the wrong "filename"). I think this word is written differently from programmer to programmer...
–
Ray KoopaFeb 25 '14 at 12:29

@909Niklas I'd drop "name" entirely as much as possible. Just use "street" If it's a string, it's a name unless that's detrimental or confusing. username is what it is, it's a call sign, not the "user's name", so it's more like motorcycle than firstName or lastName (which should be camelcased)
–
Peter TurnerFeb 25 '14 at 17:56

2

@PeterTurner Some programmers would say that street should be a class and name one of its attributes/data members.
–
JABFeb 25 '14 at 20:04

For what it's worth, my team and I generally use hashmap as a single word, although ironically we don't for tree map. shrug
–
corsiKaFeb 24 '14 at 21:57

1

Well, motorcycle is a bit of an edge case (it was, in fact, originally a motor cycle, though now that's something different); honeyBee (and not honeybee, in fact not "honeybee" but "honey bee"), and butterfly.
–
raxacoricofallapatoriusFeb 25 '14 at 4:08

3

The non-adressed setUp case is an edge case though, it's a single word, but JUnit historically has it's fixture initialization named setUp.
–
TC1Feb 25 '14 at 8:21

17

@TC1 Actually, "setup" is a noun, never a verb. "Set up" the correct form of the action. JUnit has it right, the rest of us are the edge case. :)
–
Jason CFeb 25 '14 at 12:49

phone number is not its own word, put it into google as a single word, it'll happily look up everything that has "Phone Number" not phonenumber. And setup isn't two words, neither is password.
–
Jeff LangemeierFeb 24 '14 at 17:38

6

@JeffLangemeier A very small quibble: “setup” is a noun, while “set up” is a verb. Hence if you have an object representing how something is set up it would be called setup, while if you had a function to set something up it would be called setUp.
–
bdeshamFeb 24 '14 at 21:51

2

@bdesham I realize that, but I would just stay away from setUp, since a better, less ambiguous verb would be initialize, I actually completely forgot about the verb version of setup.
–
Jeff LangemeierFeb 24 '14 at 22:14

1

I would think the one that starts with "wa" could either be wavelength or waveLength depending upon whether the value in question is relates to propagation velocity divided by frequency, or whether it relates to the "length" (not necessarily distance) of some kind of wave. For example, an audio synthesis library's variable for the number of samples associated with a wave would not be wavelength but waveLength.
–
supercatFeb 24 '14 at 23:09

2

@kevincline: In audio synthesis engines, it's common to have wave tables which contain one or more whole samples of a wave. When playback reaches the end of the table, it loops back either to the beginning (for simple engines) or to some other spot in the wave. To be sure, WaveLength by itself would be a bit vague in any but the most simplistic scenarios [does it refer to the buffer size or the length of the looping section] but the words could perfectly well appear in a longer name.
–
supercatFeb 25 '14 at 0:07

Part 1
I think what's tripping you up is that some of your examples are compound words, whereas others are not.

phone number // not a compound word

motorcycle // compound word

wavelength //compound word

sunblock // compound word

Sunday // compound word*

motorcycle color // compound word used with a non-compound word.

Compound words do not follow camel case notation for the second word in the compound word.

So it's motorcycle and not motorCycle.

But "phone number" is not a compound word and it would follow camel case notation. i.e. phoneNumber.

And that leads to: motorcycleColor.

If I can't recall if a word is truly a compound word or not, I look it up in the dictionary to be certain.

Part 2
Two of your examples are a little trickier.

hash map

hash table

They're tricky because some will spell them as "hashmap" and "hashtable". According to the dictionary I just checked, they are separate words and not a compound word. So the correct camel case would be hashMap and hashTable. Not every team follows that convention, so you need to ask your teammates what they prefer and then stick with that. Consistency is more important in this case than what a dictionary may say is correct.

* Some will argue about Sunday being a compound word since other days of the week are not, but for the purposes of this question I'll sidestep that issue. It's easiest to treat as a compound word and therefore no camel case.

@kevincline: Those forms of the names of the days are probably considered archaic now, so don't really count for practical purposes. ...Unless you actually write code that looks like if (currentDay == thorsDay) {... Hmm... too bad date libraries don't have that as a formatting option when you want to get the full name of the day of the week.
–
FrustratedWithFormsDesignerFeb 24 '14 at 18:14

15

@FrustratedWithFormsDesigner: the point was that in modern usage, Sunday is a compound word to the same degree that Monday is: not at all. It's just an accident that the "Sun" in "Sunday" can stand alone, while "Moon day" was shortened to "Monday"
–
kevin clineFeb 24 '14 at 18:32

5

Better yet, use better names for your variables than hashMap or hashTable. It's a slippery road that leads to this wonderful line of code: HashMap hashMap = new HashMap();
–
Eric AndresFeb 24 '14 at 22:09

As others have pointed out, camel case is used when joining words. Do not use camel case within a single English word. The problem comes in determining which compounds are single words. Left to themselves, programmers will come to different decisions depending on their knowledge of English. This leads to inconsistencies in capitalization, which are almost as annoying as misspellings.

To prevent this, programmers should use a standard reference (e.g. dictionary.com) when they are unsure about spelling or capitalization. Even so, if consistency in naming is important to your project, there is no substitute for code review.

Picking a single reference for your project will resolve most arguments about capitalization and spelling. There are some compounds like Hashtable that are not in dictionaries, but are capitalized as single words in the Java and C# APIs. You will want to make a project-wide decision about those. In Java, it's "HashMap" but "Hashtable". IMO "Hashtable" is clearly incorrect; it never appears as a single word except as a name in an API.

This is where a complication with plain English gets in the way of how one later abuses English to fit with the white-space rules of tokens in programming contexts.

Of the example words you give, almost all are compounds. Sunday isn't, as while it originated as a compound, it did so so long ago that it can scarcely be considered one now, especially as its being dedicated to the sun is more a historical curiosity than a strong part of the religious and cultural outlook of English-speaking people.

And in English, the three main ways to write a compound are open (with a space between the two words), hyphenated, and closed (with no space or hyphen between the two). And while there are some rules about hyphenating open compounds in certain situations, there isn't really much in the way of firm rules for knowing what form is used for a given compound beyond seeing what other people do, and there are still times when you'll find two or even all three forms (eggbeater, egg-beater and egg beater for example).

Here, phone number is almost always written open, so it should be treated as two words.

And when inter-capping to form a token, the space or hyphen is removed and the second and subsequent word written in title-case, hence phoneNumber or PhoneNumber as appropriate for your convention.

The thing that makes phoneNumber seem strange is probably that it's more commonly just given as phone in most contexts, or as number when the context is about a telephonic connection.

The compounding of words to represent concepts changes as we become more familiar with a given agglutination.

For example...

'telephone set' became shortened to just 'telephone'

'telephone' became shortened to just 'phone'

'phone number' was sometimes contracted into 'phone-number'

'phone-number' soon became 'phonenumber'

'phonenumber' is often further shortened to just 'phone'

'cellphone', or just 'cell' are in common usage today

'homephone', and 'workphone' may soon become common as well

Note that the StackExchange spell-checker can't make up its its mind. 'Phonenumber' at the end of the sentence above was fine but it flags the one at the beginning of the sentence (capitalized or not).

As much as most everyone desires there to be "one right way" to do things, when it comes to language, that can never be. By the time you finish your project, usage will probably have changed. Certainly your understanding will have changed and the functionality of the method/et.al. will probably have changed too. (In which case, you should probably change the name appropriately.)