Snake Case vs Camel Case vs Pascal Case vs Kebab Case

Snake Case vs Camel Case vs Pascal Case vs Kebab Case

by | 4 min read
Published:
Updated:

Coming up with names for things when writing software is hard. You need to come up with a few words that neatly describe everything you have stuffed into one class, and you also have to pick which case you will use.

There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors.

Leon Bambrick

TLDR; If you are too lazy to read my slightly humorous article and just want to know the difference, then please tattoo the following to your arm:

snake_case
camelCase
PascalCase
kebab-case

Why do we have so many different cases?

We have 4 main cases when naming variables and other things in programming: snake case, camel case, pascal case and kebab case.

They all accomplish one goal, to get rid of that pesky space character.

Why is it such a problem?

Well, imagine this was your function:

function get new package(package size, package weight)
{
	return new item(package size, package weight);
}

It doesn’t look nice, but it makes sense. Now try and write a compiler that can correctly work out what is a variable and what is a reserved keyword.

The spaces make it difficult for the computer to work out where the name of the function starts and the variables end.

It gets even harder for type-safe languages.

🚀 Are you looking to level up your engineering career?

You might like my free weekly newsletter, The Curious Engineer, where I give career advice and tackle complex engineering topics.

📨 Don't miss out on this week's issue

To make things easier, we have to get rid of the space. We could just remove the space, but that makes things very difficult to read.

function getnewpackage(packagesize, packageweight)
{
	return new item(packagesize, packageweight);
}

Do we mean “get new pack age”, or is it a “pac kage”?

It is clear that we need a way to separate the words without using a space, hence all the different cases.

If we don’t split out the words, it can have a lot of unintended consequences. Take these domains for example:

  • Who Represents - whorepresents.com
  • Pen Island - penisland.net
  • Speed of Art - speedofart.com
  • Old Man’s Haven - oldmanshaven.com

Clearly, they could have benefited from one of these cases we are going to look at.

What does a snake, a camel, a French mathematician and a kebab have in common?

Snake Case (snake_case)

In our first attempt to banish that space between letters, we are going to replace it with the underscore character _.

This makes our variable names look like this this_is_a_variable.

Snake

Clearly whoever came up with this name has never seen a snake.

It would make more sense to be called “battlement case” like the top of a castle or the Great Wall of China.

Great Wall of China

I have heard the underscore is supposed to represent the snake, either way, that person needs to get out more.

Snake case is popular with those writing in Python, maybe they just have a thing about snakes.

It is also frequently used for API contracts (take a look at [Stripe’s API docs] for example).

I always tend to use snake case for my APIs contracts and I often use SCREAMING_SNAKE_CASE for constants, which is popular if you are writing in C.

Camel Case (camelCase)

If you get rid of the space completely and start each word after the first one with a capital letter, you have what we call camelCase.

If you squint your eyes tight and then punch yourself in the face, it kind of looks like a camel…..

Camel

Camel case is what I tend to use the most for variable names and function names. Although that is because I come from a predominantly Microsoft background where that style is popular.

Camel case is a good choice for most things, but it does get confusing if you have a variable name that includes an acronym that would normally be in capitals. For example, httpSslRequest just looks wrong, but it is better to stay consistent than lose sleep over it.

Pascal Case (PascalCase)

PascalCase is similar to camel case, except we capitalise the first letter as well. It is often mixed up with camel case, as people don’t understand the difference.

Pascal Case was originally invented in 1813 by Bertlius, a Swedish chemist, for naming chemical elements (e.g. NaCl).

The reason it is called Pascal Case is that it was popular among developers writing in Pascal, which ironically is case-insensitive.

Microsoft recommends using Pascal Case for constants, although I much prefer SCREAMING_SNAKE_CASE as it is more fun to say.

Kebab Case (kebab-case)

Let us continue with the ridiculous named cases (naming things is really hard after all).

If we replace spaces with a hyphen “-“ then apparently that looks like meat skewered on a kebab stick.

Kebab

No, it just means you have been stuck on a coding problem for too long and forgotten to eat.

You will find kebab-case commonly used in URLs, but any sane programmer is unlikely to use it beyond that (unless they are starving).

Final Thoughts

Clearly, as programmers we suck at naming things, but hopefully, you should now know the difference between each of the different styles.

If you are writing code for yourself, then it really doesn’t matter too much which one you pick. Different languages have different preferences, so it would make sense to use the one that is used by most developers.

If you are working on a team, then you will need to be consistent with the style that everyone else is using. Developers are like everyone else and will happily stoop to pettiness and revenge if you use snake case instead of camel case in your code.

However, if you decide to use kebab-case for naming your variables, you deserve everything you get.


🙏 Was this helpful? If you want to say thanks, I love coffee ☕️ , any support is appreciated.


ALSO ON ALEXHYETT.COM

Idempotency - What it is and How to Implement it

Idempotency - What it is and How to Implement it

  • 22 September 2023
When designing an API it is easy to think about the happy path of our applications but if you want to build a robust application and keep…
5 Design Patterns That Are ACTUALLY Used By Developers

5 Design Patterns That Are ACTUALLY Used By Developers

  • 08 September 2023
High-level programming languages have been around since the 1950s and since then programmers worldwide have been using code to solve all…
Domain-Driven Design: Simple Explanation

Domain-Driven Design: Simple Explanation

  • 28 April 2023
When you are trying to build complex software it is important that everyone is on the same page. Even though most of us prefer to work alone…
Monolithic vs Microservice Architecture - Which Should You Use?

Monolithic vs Microservice Architecture - Which Should You Use?

  • 17 March 2023
If you are starting to build a new application or you are working on an existing one you may be wondering whether you should be building a…
Hexagonal Architecture: What Is It and Why Do You Need It?

Hexagonal Architecture: What Is It and Why Do You Need It?

  • 17 February 2023
We all do our best to try and write clean code that is going to be easy to maintain in the future. As time goes on and the application gets…
What is CRUD? CRUD Operations in APIs

What is CRUD? CRUD Operations in APIs

  • 01 February 2023
90% of software that you use every day are what we call CRUD applications. Most of the time we are using them without even realising it…
Why Developers Should Embrace No-Code Solutions

Why Developers Should Embrace No-Code Solutions

  • 09 January 2023
The best line of code is the one you didn’t have to write. As developers, we love writing code. At the end of the day, it is what we are…
Adding Inline Javascript to a React App

Adding Inline Javascript to a React App

  • 11 February 2022
Let me start this with a disclaimer, I am not a React expert and I am sure there are probably other ways to do this. If you know of a better…