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