Start learning free

LESSON 11 OF 414 · VARIABLES

Variable Naming Rules in Scala: camelCase and Legal Names

Lesson Read the full explanation

TL ; DR:

  • In Scala, 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 = "hello"
val #name = true

If you try to create a variable with any of these names, Scala 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 Scala 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)

Scala 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

    SCALA · VARIABLES

    Variable Naming Rules in Scala, explained

    What a legal name looks like

    A name in Scala is built from letters, digits and underscores, and it can't begin with a digit. Names also can't contain spaces, dots or symbols such as #, and they can't be a reserved word like val, class or if. These declarations are rejected:

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

    The compiler reports a syntax error for each. For first name it reads the word first as the name and then expects an =, but finds name:

    -- [E040] Syntax Error ----------------------------------------------------
      |  val first name = "Kate"
      |            ^^^^
      |            '=' expected, but identifier found
    

    Scala is more permissive than this short rule suggests. Letters from other alphabets are accepted, and a name written in backticks can contain nearly anything, including spaces. Both are rare in ordinary code. Stick to the plain rule above.

    The camelCase convention

    When a name needs several words, you can't separate them with spaces. Scala programmers join them and capitalise every word after the first. This is called camelCase:

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

    Underscores are legal too, so first_name compiles. This style is called snake_case. Scala code uses camelCase by convention, and the lesson's exercise asks for it. What matters most is consistency: don't mix first_name and lastName in the same program.

    Case sensitivity

    Upper and lower case letters count as different characters, so these are three separate values:

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

    Scala allows this, but telling variables apart by capitals alone is confusing. Pick different words instead. Capitalised names are also conventionally kept for classes and similar definitions, not for ordinary variables.

    Names that explain themselves

    The compiler is equally happy with a, b and c as with descriptive names, but a reader isn't:

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

    This prints 325, and each name says what it holds. Short names such as i or x are fine for tiny scopes, like a loop counter. For anything that matters, use a name that tells the reader what the value means.

    Booleans read best as questions: isEmployed, hasLicense. Then code such as if (isEmployed) reads almost like English.

    The exercise step by step

    1. A first name. Declare firstName with val and the string "John". The first word is lowercase, the second starts with a capital.
    2. A yes or no. Declare isEmployed with val and the value true. The is prefix signals a boolean.
    3. A number. Declare yearsOfExperience with val and the value 5. It has three words: years, Of and Experience, joined with capitals.

    The solution in Scala

    val firstName = "John"
    val isEmployed = true
    val yearsOfExperience = 5
    

    The three lines use val, which declares a name that can't be reassigned later. Use var only when the value must change. Scala infers the types here: String, Boolean and Int.

    Common mistakes

    • A space in the name. first name is two words to the compiler.
    • Starting with a digit. 18spam is not a legal name; spam18 is.
    • Wrong capital letters. firstname, FirstName and firstName are three different names. Using one spelling to declare and another to read gives a "Not found" error.
    • Mixing styles. Choose camelCase and keep it throughout.

    Key takeaways

    • Names use letters, digits and underscores, and don't start with a digit.
    • Scala code uses camelCase: lowercase first word, capital letter for each next word.
    • Names are case sensitive.
    • Pick names that describe the value, and start boolean names with is or has.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free