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.
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:
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:
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.
Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com