Skip to Content
Course content

5: Values, Variables, and Types

Click on the "Edit" button in the top corner of the screen to edit your slide content.

When you're first diving into Scala, coming from a background in Java, Python, or JavaScript, your instinct is probably to reach for a variable whenever you need to store data. In those languages, "variable" is the default. You just declare it and move on. But in Scala, we approach state differently. If you treat every piece of data as something that can change, you're essentially building a minefield for your future self.

The Danger of Mutability by Default

Let's look at a naive way to handle a simple game character's stats. If I were writing this without thinking about Scala's philosophy, I might do something like this:

var characterName = "Eldrin the Bold"
var characterLevel = 1
var characterHealth = 100

On the surface, this works. It's intuitive. But here is the problem: I've told the compiler that the character's name can change at any time. Why would a name change mid-game? Maybe for a plot twist, sure, but in 99% of cases, it shouldn't. By using var for everything, I've lost the ability to reason about my code. If I find a bug where the name is suddenly empty or corrupted, I have to search every single line of my program to find where that var was reassigned.

The Safety of the Immutable Contract

The better way—the "Scala way"—is to default to val. A val is an immutable reference. Once you assign it, it's locked. If you try to change it, the code won't even compile. It's not a limitation; it's a guarantee.

val characterName = "Eldrin the Bold" // This will never change
var characterLevel = 1                  // This will grow
var characterHealth = 100               // This will fluctuate

Now, I've created a contract. I can look at characterName anywhere in my project and know with absolute certainty that it is still "Eldrin the Bold". I've reduced the "surface area" for bugs. I only use var for the things that truly need to change, like health or level. If you find yourself using var for everything, stop and ask: "Does this actually need to change, or am I just used to doing it this way?"

When to be Explicit with Types

You probably noticed that I didn't write val characterName: String = "Eldrin the Bold". I let Scala figure it out. This is type inference, and it's great for keeping code clean. However, there's a trade-off. When you're writing a small internal logic block, let the compiler guess. But when you're defining the "public face" of your code—like a method signature or a class property—I highly recommend being explicit.

Consider this difference:

// Inferred - fine for local variables
val damage = 15 

// Explicit - better for documentation and API stability
val baseAttackPower: Int = 50

When I see baseAttackPower: Int, I don't have to hunt through the initialization logic to figure out what I'm dealing with. It serves as a form of documentation that the compiler actually enforces. If I accidentally try to assign a Double to that Int, the compiler catches it immediately, rather than letting a precision error sneak into my combat calculations.




📋 Practical Task

Implementing a Mutable Inventory Ledger

You need to create a small program that manages a player's inventory item. Some data about an item is permanent (the item's unique ID and its name), while other data changes as the player uses it (the current quantity and the durability percentage).

Your goals:

  • Declare the itemID and itemName using the most restrictive keyword possible to ensure they cannot be changed after initialization.
  • Declare the quantity and durability using the appropriate keyword to allow them to be updated as the game progresses.
  • Explicitly define the types for all four values to ensure there is no ambiguity about whether they are Integers, Strings, or Doubles.
  • Write a few lines of code that update the quantity and durability, and then try to update the itemName to see the compiler stop you.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.