views:

504

answers:

9

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?

+4  A: 

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.

popester
Also .NET adopted `ToString()` as a convention, instead of `toString()`, contrary to Java.
Joey
Ok, but why would lower casing the first word of a method name make it easier to read?
Jeremy
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
+1  A: 

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.

kurosch
gee thanks for the drive-by anonymous downvote to my perfectly valid, yet mildly sarcastic, observation
kurosch
+1  A: 

I don't think there is any reason, these are just conventions and everyone might have his own.

M4N
+1  A: 

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)

Brian Agnew
In modern IDEs with IntelliSense, this is no longer true.
SLaks
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
+1  A: 

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.

Lou Franco
+3  A: 

Because that's what the original designers of Java liked.

SLaks
Yes by why? Usually you'd do something, I would think, to serve a purpose...
Jeremy
Because they prefer that way.
SLaks
+5  A: 

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.

Bill
A: 

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.

Chris S
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
+2  A: 

We use lower-case on the first letter to save a little ink in our printouts.

Bruce