问题
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?
回答1:
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.
回答2:
We use lower-case on the first letter to save a little ink in our printouts.
回答3:
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.
回答4:
Because that's what the original designers of Java liked.
回答5:
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.
回答6:
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.
回答7:
I don't think there is any reason, these are just conventions and everyone might have his own.
回答8:
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)
回答9:
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.
回答10:
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.
回答11:
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
https://dotnetprod.bsu.edu/AdminConsole/Documentation/ASPDotNet/CodingStyle/NamingConventions/Capitalization.aspx
回答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!)
来源:https://stackoverflow.com/questions/1609440/camel-case-method-names