Naming Conventions: Camel Case vs Pascal Case vs Snake Case vs Kebab Case

Compare camel case, Pascal case, snake case and kebab case with examples. Learn which styles suit Java, Python, URLs, CSS and constant names.

The same name written as totalInvoiceAmount, TotalInvoiceAmount, total_invoice_amount, total-invoice-amount and TOTAL_INVOICE_AMOUNT

Naming conventions describe how we join words in names such as variables, classes and files. For example, totalInvoiceAmount and totalinvoiceamount both describe a total invoice amount, but they use different conventions to make the words readable without spaces.

Camel case joins words and capitalizes each word after the first. Pascal case capitalizes the first word too. Snake case separates words with underscores, while kebab case separates them with hyphens. The right choice depends on what we are naming and the conventions of the language or project.

The same name written as totalInvoiceAmount, TotalInvoiceAmount, total_invoice_amount, total-invoice-amount and TOTAL_INVOICE_AMOUNT

1. Compare the Naming Conventions

The overview uses the same words in every name so we can see exactly what changes.

Pascal case is also called upper camel case. When a guide uses “camel case” without further explanation, it often means lower camel case, as in totalInvoiceAmount.

These are naming conventions, not interchangeable language features. For example, total-invoice-amount is suitable for a URL path, but it cannot be a single Java variable name. Java treats the hyphens as subtraction operators.

2. Camel Case

In camel case, the first word starts with a lowercase letter. Each following word starts with a capital letter. We do not insert spaces or separators.

int itemCount = 3;
String customerName = "Asha";

int calculateTotal(int price, int quantity) {
    return price * quantity;
}

Here, itemCount and customerName are variables, and calculateTotal() is a method. Their capital letters make the word boundaries visible.

Java commonly uses this style for fields, local variables and methods. JavaScript also commonly uses it for variables and functions. An existing API may use a different convention, so we follow its established names when calling it.

3. Pascal Case

Pascal case follows the same pattern, but the first word also starts with a capital letter. Java commonly uses it for class and interface names.

class InvoiceCalculator {
    int calculateTotal(int price, int quantity) {
        return price * quantity;
    }
}

interface PaymentGateway {
    boolean pay(int amount);
}

The difference is visible in InvoiceCalculator and calculateTotal(). The class name begins with a capital letter, while the method name begins with a lowercase letter.

We should check a language’s conventions instead of assuming that every method uses camel case. C#, for example, commonly uses Pascal case for public methods as well as types.

4. Snake Case

Snake case places an underscore between words. In its usual lowercase form, every word stays lowercase.

item_count = 3
customer_name = "Asha"

def calculate_total(price, quantity):
    return price * quantity

Python’s PEP 8 naming guidance recommends this form for functions and variables, while class names usually use Pascal case.

Database tables and columns also often use snake case, such as invoice_items and customer_id. That is a database naming choice, not a requirement shared by every database or project.

5. Kebab Case

Kebab case places a hyphen between words. It is useful in web addresses and CSS class names.

<a href="/invoice-payment-history/">Payment history</a>
<div class="invoice-summary">...</div>

The URL path invoice-payment-history and the CSS class invoice-summary both use kebab case. A file such as invoice-summary.html can use it too.

We cannot use invoice-summary as an ordinary variable name in Java, JavaScript or Python. Those languages interpret the hyphen as an operator. In JavaScript, an object property can contain a hyphen when we quote its name and access it with brackets.

const invoice = { "customer-name": "Asha" };
console.log(invoice["customer-name"]);

6. Screaming Snake Case and Other Variants

Screaming snake case uses uppercase letters with underscores, such as MAXRETRYCOUNT. In Java, this convention is commonly used for constants.

private static final int MAX_RETRY_COUNT = 3;

However, adding final to a variable does not automatically mean its name should be uppercase. For example, a final local variable can still use camel case. Also, a final reference to a mutable list does not make the list immutable. A static final retry limit is a constant, while a static final mutable list normally keeps a descriptive camel-case name. Style guides may also treat deeply immutable objects as constants.

We may occasionally see names such as TOTAL-INVOICE-AMOUNT, sometimes called screaming kebab case. This does not make them valid Java identifiers. We use such variants only where the surrounding format allows them and an existing convention calls for them.

7. Choosing a Convention

We first check the rules of the language, then the project’s style guide. The Google Java Style Guide is one example of a guide that defines conventions for types, methods, constants and acronyms.

Consistency also matters at API boundaries. Renaming a JSON property from customer_id to customerId may look like a style improvement, but it can break clients that expect the original name. We should treat changes to public names as API changes.

Finally, casing cannot make an unclear name meaningful. d, D and D_VALUE still tell us less than deliveryDate. We choose descriptive words first and apply the appropriate naming convention afterward.

8. Conclusion

The word separators and capital letters tell us which naming convention a name uses. The language and the thing we are naming tell us which convention to choose. In Java, camel case for methods and variables, Pascal case for types and screaming snake case for constants provide a useful starting point.

Happy Learning!

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.