Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
6: Control Flow as Expressions
If you're coming from Java, C#, or JavaScript, you're probably used to thinking of control flow as a set of instructions—a sequence of "do this, then do that." In those languages, an if block is a statement; it performs an action but doesn't "become" anything. When I first started with Scala, I spent the first few weeks writing code that looked exactly like Java, and it was a waste of the language's best features.
The mutable "var" trap
Let's look at a common scenario: calculating the price of a subscription based on a user's tier. In a statement-based mindset, you'd probably declare a mutable variable and then update it inside your logic. It looks something like this:
var price: Double = 0.0
if (tier == "Pro") {
price = 29.99
} else if (tier == "Basic") {
price = 9.99
} else {
price = 0.0
}
// Now 'price' is ready to be used
This works, but it's brittle. I've seen this pattern lead to bugs in huge codebases because someone accidentally modifies price somewhere between the declaration and the final usage. You're forced to use var, which means you've just introduced mutable state into your function. Every time I see a var used just to get a value out of an if block, I feel a slight twitch in my eye. It's just unnecessary risk.
Leveraging the return value
In Scala, if is an expression, not a statement. This means it returns a value. You don't need to update a variable; you can just assign the result of the entire if/else block to a val. This is a fundamental shift in how you should approach your logic.
val price = if (tier == "Pro") {
29.99
} else if (tier == "Basic") {
9.99
} else {
0.0
}
Notice that I swapped var for val. The value is determined at the moment of assignment and can never change. It's cleaner, it's thread-safe, and it tells anyone reading your code: "This value is decided once and stays this way." I personally find this makes debugging significantly easier because you don't have to scan the rest of the method to see if the price was tweaked by some other conditional block further down.
When if becomes too noisy
While if as an expression is great, it starts to get clunky when you have more than two or three conditions. That's where pattern matching comes in. Just like if, a match block is an expression. It doesn't just branch the code; it evaluates to a value.
Compare the if/else chain to this:
val price = tier match {
case "Pro" => 29.99
case "Basic" => 9.99
case "Enterprise" => 99.99
case _ => 0.0 // The 'wildcard' handles everything else
}
The logic is identical, but the intent is much clearer. We've moved from "checking conditions" to "mapping inputs to outputs." This is the "Scala way." Instead of telling the computer how to step through the logic, you're describing what the result should be based on the input. Whenever you find yourself writing an if/else chain that assigns a value, stop and ask yourself if a match expression would be more readable. Usually, the answer is yes.
📋 Practical Task
Build a Tiered Shipping Cost Calculator
You need to implement a function that determines the shipping cost for a package. Instead of using mutable variables and statements, implement this using a match expression to return a value directly to a val.
The Requirements:
- If the destination is
"Local", the cost is5.00. - If the destination is
"National", the cost is15.00. - If the destination is
"International", the cost is50.00. - For any other destination, the cost should be
0.00(representing an invalid destination).
Write a small program where you define a variable destination and use a match expression to assign the correct cost to a val shippingCost. Print the final cost to the console.
There are no comments for now.