When naming identifiers etc. there are a whole host of options open to the programmer, including randomly, but readability and consistency suggest that you should settle on one rule and keep to it. This short article discusses the variety of examples from which you might choose such a rule and gives examples of each before very briefly touching on which might be better to use.
Upper Case and Lower Case
To begin, let’s remove the most obvious types of naming conventions. Both Upper Case and Lower Case need no further explanation other than to say that often, upper case is used to denote constants in code.
Other names that this might be known as include Lisp-Case and COBOL-CASE.
Much like the animal with humps, Camel Case uses the idea that each word in a name should be differentiated by starting with a capital letter. As an example, let’s go back to the phrase “the big white bear” and transform that:
There is another variety of this and that is where the initial word begins with a lowercase letter (known as Lower Camel Case) like so:
Other names for Camel Case are Pascal Case (where the first word starts with a capital letter), Camel Caps and Medial Case. By the way. in case you’re wondering, medial means: in the middle (I had to look that up!).
As its name suggests, Underscore Case uses underscores to delimit the words. Returning to our bear example, this would lead to:
ADA has a convention which mixes both Underscore Case and Camel Case like this:
So there are a fair few ways to select from and to be honest, some of these are often dictated by custom depending on the language you use, but is there any science behind which style might be better? Actually there is.
The study described in this paper shows that although
those without training take longer to recognize identifiers in the camel case style, all subjects are more accurate when
identifying a camel-cased identifier. In addition, subjects
trained to use camel casing take less time to identify a
camel-cased identifier than an underscore identifier.
Thanks to Steven Jeuris for that reference.
Whichever you use, as already suggested, please be consistent and reference existing standards for your language.
Written by Stephen Moon
email: stephen at logicalmoon.com