81

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.

Related: https://english.stackexchange.com/questions/889/when-should-compound-words-be-written-as-one-word-with-hyphens-or-with-spaces

7 Answers7

178

You should capitalize the letter after where there would "normally" be a space (when you would write a letter instead of sourcecode). Your first assumption is correct:

  • It's a "motorcycle", not a "motor cycle", so your variable should be named motorcycle, not motorCycle.
  • It's a "hash map", so you go with hashMap, not hashmap.
  • "Sunday" is one word -> sunday, not sunDay.
  • While "day of week" would be dayOfWeek, not dayofweek.
Jack
  • 4,539
oezi
  • 1,941
45

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.

8

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.

kevin cline
  • 33,798
4

The simplest way to do camel case is on word delineation.

Examples from your question:

  • phone number: phoneNumber
  • motorcycle: motorcycle
  • sunday: sunday
  • wavelength: wavelength

If you wouldn't split it into two words, you also shouldn't use camel case on it. Otherwise you're using it on syllables and how silly does this look

  • auxIlIArY (This is a 5 syllable word; not to mention the I's looking like crap on non-monospaced/sans-serif fonts).
4

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.

Jon Hanna
  • 2,135
2

Insure consistency within the code file.

Promote consistency within the code project.

Prefer consistency within the code library.

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.)

-1

From a strictly technical (US) perspective, where xxx-yyy-zzzz is what you'd normally think of as a "phone number' the xxx part is properly referred to as the area code, the yyy part is properly referred to as the exchange, and the zzzz is properly referred to as the number.

Since not only is "phonenumber" not a compound word (yet), but if it becomes one, it will incorrectly reference something outside of its creator's intent, I would say that camelCase syntax would require phoneNumber for any variable that references it, but if you're going to do anything with the actual telephone system, I'd use phoneExchangeNumber for the seven-digit and phoneACExchangeNumber for the ten-digit versions.

MLT
  • 1
  • 1