Camel Case vs. Snake Case

In the programming world, we distinguish two different naming conventions – Camel Case and Snake Case. We use these conventions when naming classes, variables, methods, packages, enums, interfaces, etc.

Now, the choice between Camel Case and Snake Case often depends on the specific coding guidelines of the programming language or a project you are currently working on. However, it is important to be consistent in naming conventions within the codebase in order not to impair the readability of the code.

In this short tutorial, we will learn the difference between Camel Case and Snake Case naming conventions.

1. Camel Case

Camel Case (or camelCase) is a naming convention where words are joined together. Furthermore, each word, except the first one, starts with a capital letter.

Additionally, this standard is used in languages like Java, JavaScript, and C#. However, it is important to note this is only the recommended way to write your code – you can still use other naming conventions and your code will still compile just fine.

1.1. Classes

When naming classes, we should capitalize the first letter of the first word as well.

Now, let’s write a class using the camel case naming convention standard:

class Animal {
}

Additionally, if we have two words, the name of the class would look like the following:

class PetCenter {
}

1.2. Variables and Methods

When we are defining variables and methods, the rule applies as is:

public class Animal {

    int age;
    int numberOfLives;

    public void eat() { 
        System.out.println("I'm eating"); 
    } 

    public void makeSound() { 
        System.out.println("Meow"); 
    }
}

2. Snake Case

On the other hand, the Snake Case is the naming convention where words are separated by underscores (_). Moreover, all letters are typically in lowercase.

We use the snake case convention in languages such as Python, Ruby, and SQL for variable and function names.

2.1. Classes

Here, unlike with Camel Case, class names start with the lowercase:

class animal {
}

The same rule applies to multi-word class names:

class pet_clinic {
}

2.2. Variables and Methods

Variables and methods are all in lowercase:

public class animal {
    int age;
    int number_of_lives;

    public void eat() {
        System.out.println("I'm eating");
    }

    public void make_sound() {
        System.out.println("Meow");
    }
}

3. Key Takeaways

In this tutorial, we learn the differences between Camel Case and Snake Case naming conventions.

To sum up, Camel Case starts with a lowercase letter (for variables and methods), and subsequent words start with uppercase letters, without spaces or punctuation. On the other hand, in Snake Case words are separated by underscores and all letters are in lowercase.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x