views:

110

answers:

4

Most languages give guidelines to separate different words of a name by underscores (python, C etc.) or by camel-casing (Java). However the problem is when to consider the names as separate. The options are:

1) Do it at every instance when separate words from the English dictionary occur e.g. create_gui(), recv_msg(), createGui(), recvMsg() etc.

2) Use some intuition to decide when to do this and when not to do this e.g. recvmsg() is OK, but its better to have create_gui() .

What is this intuition?

The question looks trivial. But it presents a problem which is common and takes at least 5 seconds for each instance whenever it appears.

+7  A: 

I always do your option 1, and as far as I can tell, all modern frameworks do.

One thing that comes to mind that just sticks names together is the standard C library. But its function names are often pretty cryptic anyway.

Thomas
This. "Modern" is the key. Unless you have good reason to deviate, follow the two Java maxims: (1) don't abbreviate, and (2) don't jam words together. receiveMessage() is even better.
John Kugelman
A: 

For me, and this is just me, I prefer to follow whatever is conventional for the language, thus camelCase for Java and C++, underscore for C and SQL.

But whatever you do, be consistent within any source file or project. The reader of your code will thank you; seeing an identifier that is inconsistent with most others makes the reader pause and ask "is something different going on with this identifier? Is there something here I should be noticing?"

Or in other words, follow the Principal of Least Surprise.

Edit: This got downmodded why??

tpdi
Probably because it didn't answer the question? The question wasn't about whether camelCase or under_scores is better; it was about where to put word breaks.
Joe White
A: 

Just follow coding style, such moments usually well described.

For example:

ClassNamesInCamelNotaionWithFirstLetterCapitalized
classMethod()
classMember
CONSTANTS_IN_UPPERCASE_WITH_UNDERSCORE
local_variables_in_lowercase_with_underscores
Sergei Stolyarov
I don't see why your answer got down voted, so modded up in compensation.
tpdi
+1  A: 

I'm probably biased as an Objective-C programmer, where things tend to be quite spelled out, but I'd never have a method like recvMsg. It would be receiveMessage (and the first parameter should be of type Message; if it's a string, then it should be receiveString or possibly receiveMessageString depending on context). When you spell things out this way, I think the question tends to go away. You would never say receivemessage.

The only time I abbreviate is when the abbreviation is more clear than the full version. createGUI is good because "GUI" (gooey) is the common way we say it in English. createGraphicalUserInterface is actually more confusing, so should be avoided.

So to the original question, I believe #1 is best, but coupled with an opposition to unclear abbreviations.

One of the most foolish naming choices ever made in Unix was creat(), making a nonsense word to save one keystroke. Code is written once and read many times, so it should be biased towards ease of reading rather than writing.

Rob Napier