camelCase vs under_score

Original author: Sarmad Khalid
  • Transfer
Currently, there are many standards for naming variables, but two of them are the most popular among programmers: this is the camel case (“Camel” notation) and underscore (naming variables using the underscore as a separator). Someone may argue that there are other popular standards, but within the framework of this article we will compare these two and find out from programmers what standard they adhere to. Of course, some programmers are bound by the coding standards of the language or framework they use, but we will try to make an independent comparison.


The standard for naming with the underscore is that all words are written in lowercase letters and separated by the underscore (_). Usually this standard is used in the names of functions and variables, and for the names of classes, structures, and interfaces, the Pascal standard is used (translator's note: for some reason, the article does not say anything about it, but as I found in Pascal style Wikipedia, this is UpperCamelCase, when the first the word begins with a capital letter, proof) Underscore was initially used by C programmers, and then was used by C ++ developers to name keywords in the STL library, and then in Boost, which is why this standard has gained great popularity among C ++ programmers, but has not become dominant. This standard is also used in the names of standard PHP functions, and is very popular among PHP programmers. Ruby is also said to use underscores.

Camel notation, on the other hand, is the standard in the Java language and its younger sister JavaScript, although it can also be found elsewhere. According to this standard, all words in the name begin with a capital letter, except for the first. In this case, of course, no delimiters like underscores are used. Typically, this standard is applied to function and variable names, and the Pascal standard is used in class names (structures, interfaces). C # camelCase is used partially: its scope is limited to function parameter names and local variables.

Each of these two standards has its own strengths and weaknesses. Let's list the main ones:
  • The underscore is better read: compare the standard with the underscore and the standard
  • But the camel case makes it easier to read strings , for example:
    my_first_var = my_second_var-my_third_var
    and
    myFirstVar = mySecondVar-myThirdVar
    Obviously, the camel case is read better: in the case of underscores and the minus operator, at first glance the expression can be taken as one variable However, syntax highlighting can solve this problem.
  • Camel Case is the opposite of English, Russian and many other languages: in ordinary languages, a sentence begins with a capital letter, and all other words are written in lowercase, in the case of camel case, everything happens the other way around. This is a real brain explosion.
  • Underline is harder to type. Even with intellisense, in many cases you need to type an underscore.
  • The Camel Case is inconsistent because when using constants (which are written entirely in capital letters) we have to use the underscore. On the other hand, the naming standard using underscores can be complete if you decide to use underscores as separators in the names of classes (structures, interfaces)




From a translator: I tried to translate the article as close as possible to the original.
On my own, I’ll add that in my projects I prefer to use the Camel Case, I only use the abbreviated variable type (bProductActive, iProductsCount, sUserName) as the first word, this way the problem of “brain explosion” is somewhat solved - all sentence words begin with a capital letter.

In addition, there was a lot of controversy with colleagues on the topic of abbreviations when using camel notation: how to write more correctly GenerateHTMLFromText or GenerateHtmlFromText, as a result, we settled on the second option, but the feeling of an unresolved problem still gnaws a bit.

Well, about the fact that JavaScript is the younger sister of Java, the author caught up a bit, after all, these are completely different languages, they only have a name and code design standards in common.

Also popular now: