Start learning free

LESSON 11 OF 348 · VARIABLES

Variable Naming Rules in Kotlin: Rules and camelCase

Lesson Read the full explanation

TL ; DR:

  • In Kotlin, we use the camelCase convention to name our variables. It implies separating different words with capital letters, like this:

    val companyName = "Apple"
    
    val foundingYear = 1976
    
    val isUnicorn = true
    
    val numberOfEmployees = 137000
    



Full lesson:

Variable names must consist ONLY of letters, numbers and underscores and they must start with a letter or underscore.

Bad variable names:

val 18spam = 1
val x.15 = "text"
val #name = true

If you try to create a variable with any of these names, Kotlin will throw an error.


Naming conventions: camelCase and snake_case

Sometimes a variable name might consist of multiple words, for example if you want to store the first name of a person. You might be tempted to separate those names by whitespaces, like this:

val first name = "Kate"

This code will also throw an error.

When you name your variables, you want to use one of these two conventions:

snake_case = separate different words with an underscore (_), like this:

val first_name = "Jane"
val last_name = "Doe"

camelCase = separate different words with capital letters, like this:

val firstName = "Jane"
val lastName = "Doe"

It's important to be consistent with the casing technique you use. Do not combine both of them in the same code, like this:

val first_name = "Jane"
val lastName = "Doe"

Case sensitive:

Variable names are case sensitive:

val name = "Kate"
val Name = "Andy"
val NAME = "George"

println("$name $Name $NAME") // Output: Kate Andy George

Although Kotlin allows us to do this, it's always confusing and a bad coding practice to differentiate variable names just by case!


Mnemonic Variable Names:

Since we programmers are given a choice in how we choose our variable names, there is a bit of "best practice".

Sometimes we want them short like x and y, but sometimes we want them descriptive, especially if they are important.

Here is a great example:

val a = 25
val b = 13
val c = a * b
println(c)

compared to:

val hours = 25
val rate = 13
val pay = hours * rate
println(pay)

Kotlin understands and is happy with both of these pieces of code. But it's clearly which version makes your reader happier.


Assignment
Follow the Coding Tutorial and let's let's name some variables well!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    KOTLIN · VARIABLES

    Variable Naming Rules in Kotlin, explained

    Two kinds of rules

    Naming a variable involves two sets of rules. The first set is enforced by the compiler: break one and the program doesn't build. The second is convention: the code compiles either way, but other Kotlin programmers expect the habit, and sticking to it makes code easier to read.

    What the compiler requires

    A variable name may contain letters, digits and underscores, and it must not start with a digit. These names are valid:

    val count2 = 2
    val _count = 1
    val first_name = "Jane"
    

    These are not:

    val 18spam = 1
    val x.15 = "text"
    val #name = true
    val first name = "Kate"
    

    The first starts with a digit. The second contains a dot, which Kotlin reads as member access, and the third a #, which can't appear in a name. The last one has a space, so Kotlin sees first as the name and then doesn't know what name is doing there. Each of them produces syntax errors, often several at once, such as Expecting property name or receiver type and Unexpected tokens. When you see messages like these, check the name you wrote on that line before anything else.

    Words with a fixed meaning in the language, such as class, fun, val or if, can't serve as names either. val class = 1 fails to compile.

    Names are also case-sensitive. These are three separate variables:

    val name = "Kate"
    val Name = "Andy"
    val NAME = "George"
    
    println("$name $Name $NAME")
    
    Kate Andy George
    

    The compiler is happy, but a reader isn't. Never use names that differ only by case.

    Two other facts about the rules. Letters are not limited to English ones: val größe = 3 compiles. And Kotlin has a way to put almost any characters in a name by wrapping it in backticks, such as `first name`, which is then referred to with the backticks each time. It exists mostly for unusual cases like test names, and it is not something to use for ordinary variables.

    Conventions: camelCase

    When a name is made of several words, you need some way to show where one ends and the next begins, since spaces are not allowed. Two styles are common:

    • camelCase: firstName, lastName. The first word is lowercase and each following word starts with a capital letter.
    • snake_case: first_name, last_name. Words are separated by underscores.

    Both compile. Kotlin code uses camelCase for variables and functions, so this lesson does too. What matters most is being consistent. Do not mix the two in one program:

    val first_name = "Jane"
    val lastName = "Doe"
    

    This runs, but a reader has to wonder whether the difference means something. It doesn't, and that's the problem.

    A few related habits:

    • Boolean variables read well when they sound like a yes/no question: isEmployed, hasLicense.
    • Pick a name that says what the value is. Compare:
    val a = 25
    val b = 13
    val c = a * b
    println(c)
    
    val hours = 25
    val rate = 13
    val pay = hours * rate
    println(pay)
    

    Both programs print 325, and the compiler treats them identically. Only the second tells you what the number means. Short names such as x and i are fine for tiny, obvious scopes. Important values deserve descriptive names.

    The exercise

    Three variables, all declared with val, all in camelCase:

    1. firstName, set to "John". Two words, so the second one starts with a capital letter: firstName, not firstname or first_name.
    2. isEmployed, set to true. The name asks a yes/no question and the value answers it.
    3. yearsOfExperience, set to 5. Three words, with a capital letter on each word after the first.
    val firstName = "John"
    val isEmployed = true
    val yearsOfExperience = 5
    

    Spell the names exactly. A name with different capitals is another variable.

    Key takeaways

    • Names use letters, digits and underscores, and can't start with a digit.
    • No spaces, dots or symbols such as #, and no language keywords.
    • Case matters: name and Name are different variables.
    • Kotlin style is camelCase, applied consistently.
    • Choose names that explain the value.
    • Fixed values that never change are often written in UPPER_SNAKE_CASE in larger programs; for ordinary variables, stay with camelCase.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free