What Is Snake Case? Definition & Alternatives (with Examples)

What Is Snake Case?

Snake case is a popular naming convention for combining multiple words in coding. In snake case, the compound words are separated by underscores. This type of naming convention is used to make code more readable. In programming, you cannot use spaces to separate words. This would cause an error. Thus, alternative separation styles, such as the snake case exist. Here are some examples of the snake case:

  1. my_age
  2. last_login_time
  3. MAX_CONNECTIONS_ALLOWED

Snake case is typically used in naming constants. In this case, the letters are typically capitalized.

For example:

PI_APPROX = 3.14159
MAX_CONNECTIONS = 32

Another popular use case for the snake case is labeling the database fields.

For example:

{
    username: "Alice",
    user_login_attempts: 13,
    last_attempt: 1662988728,
}

But the naming conventions vary based on coding languages and team preferences. Some Python developers write variables, functions, and method names in snake case.

What Problem Does Snake Case Solve?

In programming, you need to write quality code that is readable and easy to manage. One of the fundamental ways to write clean code is by naming objects consistently. Typically, this means you need to combine multiple words to make the names understandable.

But programming languages don’t allow using spaces.

Usually, you need a name that combines multiple words. For example, mybankaccountbalance. But this name is long and uneasy for eyes. Because of the restrictions in coding languages, you cannot introduce spaces to separate the words.

This is where different case styles chime in. One of the most popular case styles is the snake case. In the snake case, the words are separated by underscores. This naming convention makes long combinations of words much more readable.

For example:

  • mybankaccountbalance –> my_bank_account_balance

Why Is It Called the Snake Case?

The snake case term stems from the fact that snake case makes the words look like the long body of a snake. The earliest use of this term took place back in 2004. Back in the day, the term camel case was already a thing. The word snake case came to being probably because of the animal association of the camel case.

Other Case Styles

Even though snake case is popular, it’s not the only case style out there. There are four popular case styles used by programmers:

  • Snake Case
  • Pascal Case
  • Camel Case
  • Kebab Case

For the sake of completeness, let’s inspect the last three case styles.

1. Camel Case (camelCase)

In camel case, compound words begin with capital letters. The only exception is the first word that begins with a lowercase letter.

For example, here is a variable in the camel case:

myBankBalance = 1000

In coding, you typically see the camel case as the convention for naming functions, methods, and variables. But these might vary based on the programming language and context.

There is no right or wrong naming convention as long as the team consistently applies the same conventions throughout the project.

2. Pascal Case (PascalCase)

In Pascal case, each word in a combination of compound words starts with a capital letter. This is very similar to the camel case.

Another way to put it is that the Pascal case is camel case where the first letter is also capitalized.

For example:

MyBankBalance = 100

Pascal case is typically used when naming classes. But remember that the conventions vary based on the programming language and the context.

3. Kebab Case (kebab-case)

In kebab case, compound words are separated by dashes.

my-bank-balance = 101

Kebab case is a less popular case style in programming. This is mainly because most programming languages don’t allow adding dashes between words.

Because of this, you usually see kebab case in URL slugs. For example:

https://bank.example.com/my-account-balance

Wrap Up

Today you learned what is the snake case in programming.

To take home, the snake case is a naming convention that helps make your code easier for the eyes. With the snake case, you can separate compound words by underscores. The reason for the snake case and other case styles is that programming languages don’t allow using spaces as separators.

Besides snake case, other popular case styles are:

  • Camel case (camelCase)
  • Pascal case (PascalCase)
  • Kebab case (kebab-case)

All these styles serve the same purpose—to make code understandable in the absence of blank spaces.

Thanks for reading. Happy coding!

Read Also