There used to be many naming conventions back in the days, but lately it seems that two conventions are emerging as the two most popular among programmers. Underscore naming convention and camel case naming convention. Someone might argue that there are other popular conventions, but for now let’s compare these two and ask programmers out there which one they prefer. In the end, a programmer might be bound to whatever is the standard for the language he is using, but this doesn’t prevent us from doing an out-of-context comparison.
Underscore naming convention uses all lower case words, and separates them with underscores. The common is to use this for variable and function names, and to use Pascal convention for classes, structures, enumerations, and interfaces. Underscores were used by C programmers, then C++ adopted the same convention for its keywords and Standard Templates Library, and later on Boost used the same convention, making this convention quite popular among C++ programmers, but not dominant as other programmers used various other conventions including Pascal and camel case conventions. Underscore convention is also used by PHP’s standard libraries and is pretty much the standard for PHP developers. Ruby also uses underscores, or so I heard.
Of course, each of the two conventions has its own strengths and weaknesses. The following list summarizes the strengths and weaknesses of the two:
- Underscores make phrases easier to read. Compare the
underscore_naming_convention to the
- Camel case makes paragraphs easier to read! Of course, when you read code, what’s important for you is to understand the overall operation or algorithm, not the words in a phrase. Compare the following:
Obviously in this case, the camel case is easier to read. From the first glance, you realize it’s a subtraction between two variables, while in the case of underscores you need to concentrate before you realize where the minus sign is. You may even mistake it for being one long variable instead of two. This is a strong disadvantage for the underscore convention, however, coloring can solve this problem if the editor gives identifiers a different color from operators, but that’s dependent on the editor.
- Camel case is the negative of English. In English, the first word in a sentence starts with an upper case and the rest remains in lower case. This is probably true for many other human languages. Camel case is the exact opposite. This is mind itching indeed.
- Underscores are harder to type. Even with the availability of intellisense, you still need to type the underscore in many cases.
- Camel case is not consistent because with constants (which is all in upper case), you still need the underscores. On the other hard, the underscore convention CAN be consistent (if you decide to use underscores even for type names, which is doable, i.e.,
My_Type instead of
Any more pros and cons you can think of? Please let us know.
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
Sarmad Khalid is a senior software engineer specialized in .NET, LAMP, and C++ groups of technologies. He has long experience developing web and desktop applications for several clients across North America and the Middle East.