Start learning free

LESSON 11 OF 415 · VARIABLES

Variable Naming Rules in Swift: camelCase and Valid Names

Lesson Read the full explanation

TL ; DR:

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

    let companyName = "Apple"
    
    let foundingYear = 1976
    
    let isUnicorn = true
    
    let 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:

let 18spam = 1
let x.15 = "hello"
let #name = true

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

let first name = "Kate"

This code will also fail to compile with 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:

let first_name = "Jane"
let last_name = "Doe"

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

let firstName = "Jane"
let 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:

let first_name = "Jane"
let lastName = "Doe"

Case sensitive:

Variable names are case sensitive:

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

print(name, Name, NAME) // Output: Kate Andy George

Although Swift 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:

let a = 25
let b = 13
let c = a * b
print(c)

compared to:

let hours = 25
let rate = 13
let pay = hours * rate
print(pay)

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


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


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    SWIFT · VARIABLES

    Variable Naming Rules in Swift, explained

    The rules the compiler enforces

    A Swift name can contain letters, digits and underscores, and it cannot start with a digit. Spaces and symbols such as . or # are not allowed. Break a rule and the code does not compile:

    let 18spam: Int = 1
    
    error: 's' is not a valid digit in integer literal
    error: expected pattern
    

    Swift reads the leading 18 as the start of a number, so it never sees a name. A space splits one name into two, which is also an error:

    let first name = "Kate"
    
    error: found an unexpected second identifier in constant declaration; is there an accidental break?
    

    Names are also case-sensitive, so name, Name and NAME are three different constants:

    let name = "Kate"
    let Name = "Andy"
    let NAME = "George"
    print(name, Name, NAME)
    
    Kate Andy George
    

    That is legal but confusing, and a bad habit. Tell names apart by their meaning, not their capitals.

    Conventions for multi-word names

    Because spaces are not allowed, a name made of several words needs another way to show where one word ends. Two styles are common:

    • camelCase: the first word is lowercase and each following word starts with a capital, as in firstName and numberOfEmployees.
    • snake_case: words are separated by underscores, as in first_name.

    Both compile. Swift code, including its own standard library, uses camelCase, and this lesson does too. Pick one style and stay with it. A program that mixes first_name and lastName runs, but it is harder to read.

    Booleans are usually named so they read like a yes/no question: isEmployed, hasLicense.

    Names should describe the data

    The compiler accepts a, b and c just as happily as descriptive names. A reader does not:

    let hours = 25
    let rate = 13
    let pay = hours * rate
    print(pay)
    
    325
    

    With hours, rate and pay, the calculation explains itself. Short names such as x and i are fine for tiny scopes, but use a descriptive name for anything that matters.

    The exercise, step by step

    1. Declare firstName with let and give it the string "John". The two words are joined with a capital N.
    2. Declare isEmployed with let and the value true. The is prefix signals a Bool.
    3. Declare yearsOfExperience with let and the value 5, in the same camelCase style.

    The solution in Swift

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

    Details to know:

    • Digits are fine after the first character. user2 is valid, and 2user is not.
    • Reserved words need backticks. Keywords like class cannot be names by themselves. Wrapping one in backticks, as in let `class` = "x", works, but choosing another name is simpler.
    • Swift allows many Unicode letters in names, for example café, but plain ASCII keeps code easy to type.
    • Types are capitalised. By convention Int, String and your own types start with a capital letter, while constants and variables start lowercase. That is one more reason to avoid Name as a variable.

    Common mistakes

    • Starting with a digit, as in 18spam.
    • Putting a space in a name, as in first name.
    • Changing the case when you use a name. Writing firstname when the constant is firstName gives a "cannot find in scope" error.
    • Mixing camelCase and snake_case in one program.

    Key takeaways

    • Use letters, digits and underscores, and never start with a digit.
    • Names are case-sensitive.
    • Write multi-word names in camelCase and stay consistent.
    • Choose names that tell the reader what the value means.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free