I understand the reason to camel case variable names, but I've always wondered why you would camel case a method name? why is it toString() and not ToString()? What purpose does it serve?
-
4Tell us what you understand to be "the reason to camel case variable names", and then maybe we can tell you "the reason to camel case a method name" :) – AakashM Oct 22 '09 at 19:35
-
1Both are actually camel case, lower or upper. It is only a convention to quickly spot what is what in your code, and so that it does look more familiar to other people who have to browse it. – RedGlyph Oct 22 '09 at 19:43
-
I assumed it was because you could declare the type with an upper case, and instantiate it with a lower case variable name, to avoid naming collisions - btw, I don't use this camel case naming convention, but I was always curious to rational behind some of it. – Jeremy Oct 22 '09 at 19:46
-
And to just easily distinguish what is a type and what is an instance. – Jeremy Oct 22 '09 at 19:47
12 Answers
A lot of conventions say you capitalize the first letter of types (classes, structs, enums, etc.), and use lowercase otherwise (functions, members, etc.).
If you follow that convention, you can then tell just by looking that MyStruct.MyType
refers to a nested type, and MyStruct.myData
refers to some form of data, and MyStruct.myFunc()
refers to a function call.

- 14,257
- 4
- 43
- 55
It's just a convention. Like all conventions they only serve to, in the minds of their creators, make code easier to read and maintain.

- 87,846
- 14
- 132
- 192

- 1,936
- 9
- 13
-
Also .NET adopted `ToString()` as a convention, instead of `toString()`, contrary to Java. – Joey Oct 22 '09 at 19:33
-
Ok, but why would lower casing the first word of a method name make it easier to read? – Jeremy Oct 22 '09 at 19:40
-
Some programmers grew up on camel casing for all of their variables, i.e.: int someInt so its somewhat of a natural progression for their methods. Just like some people will claim that Hungarian notation (http://en.wikipedia.org/wiki/Hungarian_notation) is extremely easy to read while others disagree. It's like the age old debate about whether curly braces go on the same line or next line :) – popester Oct 22 '09 at 19:50
Because to be consistent you'd have to capitalize the first letter of every method name, and then you have to hit the Shift key that many more times in a day.

- 2,292
- 16
- 17
-
gee thanks for the drive-by anonymous downvote to my perfectly valid, yet mildly sarcastic, observation – kurosch Nov 05 '09 at 14:44
Since camel cases capitalizes the first letter of each word to substitute spaces, we are left with the challenge of how to differentiate a capitalized title, like we would in English for a proper noun. As a solution to this, the first word in a camel case identifier is capitalized to indicate the title or identifier is capitalized.
In the case of programming, it seems appropriate to most, to capitalize the name of a class, but not the name of its methods. In practically it provides a nice distinction between the two.
Over the years programming has evolved to have a lot of conventions, while many are very different, there is much that people tend to agree on. However, you will find that the answers to "why questions", such as the one you posted, are not always rooted in something entirely concrete.

- 3,631
- 4
- 32
- 53
I don't think there is any reason, these are just conventions and everyone might have his own.

- 94,805
- 45
- 217
- 260
If you want a function
write();
that takes less effort (one less SHIFT keypress) than
Write();
However, if you're writing to a file, you need to distinguish the words. Hence
writeToFile();
is marginally more efficient (and still consistent with the first example)

- 268,207
- 37
- 334
- 440
-
5
-
In modern IDEs with IntelliSense, it's perhaps *less* of an issue. I wouldn't dismiss it altogether. Lots of people (rightly or wrongly) don't use IDEs (for whatever reason) – Brian Agnew Oct 22 '09 at 19:36
Usually you tend to follow the one that your framework uses. So Java developers tend to use lowercase to start, and .NET developers tend to use uppercase to start.

- 87,846
- 14
- 132
- 192
If you haven't already read the wikipedia page, it contains everything you could ever possibly want to know on camel case, including its history.
CamelCase (also spelled "camel case") or medial capitals is the practice of writing compound words or phrases in which the elements are joined without spaces, with each element's initial letter capitalized within the compound.
And
One theory for the origin of the camel case convention holds that C programmers and hackers simply found it more convenient than the standard underscore-based style.
C programmers lazy? I doubt that very much.

- 64,770
- 52
- 221
- 239
-
The underscore-based style dates back to EBCDIC, which didn't support mixed case. Variables looked like THIS_VARIABLE. Switch this to ASCII and you get this_variable, which is easier to read. Using thisVariable is even simpler, and that's what we do in case-sensitive languages. Interestingly, in case-insensitive languages, such as SQL, it's not uncommon to find underbars. – Steven Sudit Oct 22 '09 at 20:19
-
EBCDIC does support mixed case. 6-bit BCD codes were (normally?) all-uppercase, though. – David Conrad Jun 06 '13 at 21:14
Pascal case The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized. You can use Pascal case for identifiers of three of more characters. For example: BackColor
Uppercase All letters in the identifier are capitalized. Use this convention only for identifiers that consist of two or less letters, or abbreviations, such as BSU and UCS. In the example below, IO and UI are the uppercase identifiers, whereas System follows the Pascal capitalization style because the length is greater than two. For example: System.IO; System.Web.UI
Camel case The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized. For example: backColor
The following Link summarizes the capitalization rules and provides examples for the different types of identifiers and items within your Web application/project.Hopefully this link will helpful for you

- 985
- 10
- 12
A friend told me that a study showed that people could read code easier if Types were camel case, with an upper case first letter, and variables were done_like_this. It certainly does make the difference between types and variables jump out.
I've never known which was clearer for function names. I've generally considered capitalizing the first letter, but after reading this I think it might be more readable not to to distinguish between type names and method names (yes, in some languages a method signature is a type, but you know what I mean!)

- 21
- 1
-
4I am always cynical of answers that start "A friend said..." without citation. A link to the study would be good :) – Andrew Nov 03 '12 at 19:45