It’s a Matter of Case

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.

NUMBEROFPAGES=7

Kebab Case

kebabs on sticksMuch like that lovely food item, Kebab Case is the name given when words are separated by the hyphen character. Taking the example: “the big white bear”, this would create:

the-big-white-bear

Other names that this might be known as include Lisp-Case and COBOL-CASE.

Camel 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:

TheBigWhiteBear

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:

theBigWhiteBear

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!).

Underscore Case

As its name suggests, Underscore Case uses underscores to delimit the words. Returning to our bear example, this would lead to:

the_big_white_bear

Hybrid

ADA has a convention which mixes both Underscore Case and Camel Case like this:

The_Big_White_Bear

Science

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.

twittergoogle_plusredditpinteresttumblrmail

Written by Stephen Moon
email: stephen at logicalmoon.com
www: https://www.logicalmoon.com


Hey! Did you enjoy reading this? If you did and would like an email when I add new content, just subscribe to my list. You can unsubscribe at any time.


Leave a Reply

Your email address will not be published. Required fields are marked *