Identifying Variable Casing Conventions for Improved Readability

March 16, 2023#Software Development
Article
Author image.

Sarah Dutkiewicz, Senior Trainer

Naming things is hard, especially if there are multiple words that make up that name. Remembering names of naming conventions can also be hard. However, regardless of their names, these conventions can help make your code readable. Here is a guide to commonly named cases used in variable names in coding:

Capitalization-based Casing

Camel casing and Pascal casing involve words that are smashed together, without delimiters. Their casing is within the words themselves.

camelCased - Camel Casing

camelCasedName, with a camel in the background

When it comes to camel casing, think of humps on a camel. A camel’s hump follows the shape of a bell. A camel cam have multiple humps. When doing camel-cased names, the first letter is lower-cased. Each subsequent word in the variable name starts with a capital letter. Consider some of these camel-cased names:

  • orderId
  • firstName
  • dateOfBirth

PascalCased - Pascal Casing

PascalCasedName, with a keyboard in the background

Pascal casing is similar to camel casing, except the first word also starts with a capital letter. This kind of casing was popular in programs written in the Pascal programming language, hence the name. Consider some of these Pascal-cased names:

  • OrderId
  • FirstName
  • DateOfBirth

Delimited-based Casing

Kebab casing and snake casing deal with delimiters. Rather than the words smashed together, the words in the variable name are delimited with characters. They may also have some capitalization variations.

kebab-cased - Kebab Casing

kebab-cased-name, with a kebab in the background

Kebab casing puts an ASCII skewer through the words of a variable name, separating each word with dashes. Consider some of these kebab-cased names:

  • order-id
  • first-name
  • date-of-birth

Camel kebab casing is as the name suggests - the first letter of the first word is not capitalized. However, subsequent words as part of the name are capitalized. These are examples of camel-kebab-cased names:

  • order-Id
  • first-Name
  • date-Of-Birth

Pascal kebab casing is when the first letter of each word on the kebab is capitalized. These are examples of Pascal-kebab-cased variable names:

  • Order-Id
  • First-Name
  • Date-Of-Birth

Upper kebab casing - also known as screaming kebab case, COBOL case, and train case - is kebab-cased with all upper-case characters. These are examples of variables that follow this convention:

  • ORDER-ID
  • FIRST-NAME
  • DATE-OF-BIRTH

snake_cased - Snake Casing

snake_cased_name, with a snake in the background

Snake casing is similar to kebab casing, except it uses underscores instead of dashes. Consider some of these snake-cased names:

  • order_id
  • first_name
  • date_of_birth

There is a subset of snake casing called screaming snake casing or upper case snake casing. That involves the underscore character with all capital letters. These are screaming snake case examples:

  • ORDER_ID
  • FIRST_NAME
  • DATE_OF_BIRTH

Snake casing also can have camel and Pascal variations. These are camel-snake-cased variables:

  • order_Id
  • first_Name
  • date_Of_Birth

These are Pascal-snake-cased variables:

  • Order_Id
  • First_Name
  • Date_Of_Birth

Note: There are debates on capitalizing words like “of” and “the”. Whatever you choose, be consistent about it in your code base. Consistency is another key factor for readability.

The Importance of Casing

While these names may seem trivial, what do they look like without these casing conventions? What if they were in flatcase - all lower-cased characters, all words mashed together?

  • orderid
  • firstname
  • dateofbirth

If you have context around those variable names, you might be able to infer what they are at a quick glance. However, not everyone has that ability. Some folks may also rely on assistive technologies such as screen readers. Do you know how your variables’ names sound?

I was testing this out with Microsoft Narrator in Windows 11, and this is what I get:

  • dateofbirth - “day toff birth”
  • dateOfBirth - “date of birth”
  • date-of-birth - “date of birth”
  • date_of_birth - “date of underscore of underscore birth”
  • DateOfBirth - “date of birth”

So the casing choices matter - they can help the readability of the code in many forms.

I hope this helps you remember the names of these cases.

If you find yourself working with devs who need more guidance on how to write better code, consider signing up for our mentoring opportunities.


Copyright © 2024 NimblePros - All Rights Reserved