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:
firstName, set to"John". Two words, so the second one starts with a capital letter:firstName, notfirstnameorfirst_name.isEmployed, set totrue. The name asks a yes/no question and the value answers it.yearsOfExperience, set to5. 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:
nameandNameare 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.