Naming Conventions: PascalCase , camelCase, snake_case, kebab-case

What are naming conventions?

Naming conventions, as the the name states, are rules that we use to name our variables, functions, classes, etc. They help us keep our names clean and consistent throughout our code.

The most popular naming conventions are the following with no particular order:

  • PascalCase
  • camelCase
  • snake_case
  • kebab-case

You may notice that the way I've written each of the above has a different "style" to it.

Let me explain

PascalCase

PascalCase is generally used by classes, enums, etc.

In PascalCase, first letter of the word is uppercase and the rest are lowercase. Incase your name has multiple words in it, then each word's first letter will be uppercase and the rest of the letters will be lowercase.

Let's take a look at an example, usually when we make classes (in a programming language), it follows PascalCase convention.

The following example is in java

class HelloWorld {
    ...
}

The class name is HelloWorld. H of Hello and W of World are uppercase and the rest of the letters are lower case.

Another example that we can take a look at is StarCraft, name of a game and MasterCard, name of a company.

camelCase

camelCase is similar to PascalCase, with the only difference being that first letter of the name should lowercase, after that every word's first letter should be uppercase.

For example HelloWorld in PascalCase would change to helloWorld in camelCase.

Languages like JavaScript use camelCase in variable and function names.

The following example is in JavaScript.

function helloWorld() {
    ...
}

let myVariable = "I'm in camelCase";

Our function helloWorld and our variable myVariable both have their first letters lowercase and after that every other word has a capital letter.

Another popular example is iPhone.

snake_case

Every word in snake_case is separated by an underscore.

snake_case is used by languages like Python.

Let's take a look at an example in Python

my_name = "Raahim"

def hello_world:
    ...

Our variable is my_name and our function hello_world. Each word is separated by an underscore.

snake_case is also used for naming our database tables and attributes.

kebab-case

In kebab-case, each word is separated by a dash/minus (-).

Since it's a minus sign in programming languages, we don't use it to create variable names.

Instead it can be seen in urls.

For example my Rust: a bare guide URL looks like https://blog.raahim.com.pk/rust-a-bare-guide, notice rust-a-bare-guide. Every word is separated by a dash.

kebab-case is also used in css for property names and naming classes.

For example

<!-- HTML -->
<div class="my-div">I am a div :D</div>
/* CSS */
.my-div {
  background-color: #eeeeee;
}

In my-div and background-color, we can see that each word is separated by a dash.

When to use which convention?

It depends on the language you're using.

For example in C++, people use PascalCase, camelCase and snake_case all three.

In JavaScript, camelCase is preferred for variables and functions and PascalCase for classes.

In Python, snake_case is used for functions and variables and PascalCase for classes.

Conclusion

Naming conventions are used for our ease and to provide consistency to our code.

Differences between different naming conventions can be seen in the following table.

NameText
PascalCaseHelloWorld
camelCasehelloWorld
snake_casehello_world
kebab-casehello-world