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
- A first name. Declare
firstNamewithvaland the string"John". The first word is lowercase, the second starts with a capital. - A yes or no. Declare
isEmployedwithvaland the valuetrue. Theisprefix signals a boolean. - A number. Declare
yearsOfExperiencewithvaland the value5. It has three words:years,OfandExperience, 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 nameis two words to the compiler. - Starting with a digit.
18spamis not a legal name;spam18is. - Wrong capital letters.
firstname,FirstNameandfirstNameare 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
isorhas.
