Click here to Skip to main content
14,174,749 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

32.2K views
2 bookmarked
Posted 31 Mar 2010
Licenced CPOL

Underscore vs Camelcase Naming Convention

, 31 Mar 2010
Rate this:
Please Sign up or sign in to vote.
A comparison between the two most common naming conventions: underscore and camelcase conventions.

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.

On the other hand, camel case naming convention is mostly known to be the standard for Java and its little sister JavaScript, although you can find it in use in various other places. Camel case simply starts each word with an uppercase letter, except for the first word which remains in lower case. The words are not separated by any underscore, obviously. Like in the underscore convention, this applies mainly to variables and functions, while type names (classes, structures, interfaces, enumerations) remain in Pascal convention. C# uses a variation of this convention by limiting the camelcase to function parameters and local variables.

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 camelCaseNamingConvention.
  • 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:
    my_first_variable=my_second_variable-my_third_variable;
    to this:
    myFirstVariable=mySecondVariable-myThirdVariable;
    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 MyType).

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

sarmadka
Software Developer (Senior)
Canada Canada
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.

You may also be interested in...

Comments and Discussions

 
Question[My vote of 2] What about Pascal-Case? Pin
Mohammad Elsheimy31-Mar-10 4:15
memberMohammad Elsheimy31-Mar-10 4:15 
AnswerRe: [My vote of 2] What about Pascal-Case? Pin
sarmadka31-Mar-10 11:20
membersarmadka31-Mar-10 11:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190524.3 | Last Updated 31 Mar 2010
Article Copyright 2010 by sarmadka
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid