7 mistakes beginners make in scala

The fastest way to lose a week in Scala is to copy a tutorial written for a different Scala version. These seven mistakes are common because Scala gives you several valid ways to do almost everything. A beginner needs one clear path, not every path at once. Fix the habit first; the syntax gets easier after that.

1. Mixing Scala 2 and Scala 3 tutorials

A beginner finds a video using Scala 2.13, a blog using Scala 3.3 LTS, and a library that only supports one of them. The code looks almost right, but implicit examples fail while given examples are missing, or a build file asks for a plugin version that no longer exists. Pick Scala 3.3 LTS and Java 17 or 21 for your first 4-6 weeks. Write the Scala version, Java version, and build-tool version in a note beside your project. If a tutorial does not state its Scala version, skip it. One version, one REPL, one build file. Reinstall later if needed; the cost is 20-30 minutes, not a lost month.

2. Treating val and var as a style choice

val means the reference cannot be reassigned; var means it can. Beginners use var because it feels familiar, then wonder why changing a value in one method breaks another 30 lines away. Start with val everywhere. Use var only inside a 5-10 line loop when you have a measured reason, or not at all. If you need to update state, return a new value from a function. If you need a counter, keep it inside a function and return the final value. Replace a chain of 3-4 reassignments with one expression when you can. This one habit removes a large class of bugs before you learn advanced features, and it makes compiler errors more local.

3. Writing Java-style loops for every collection

Scala collections have map, filter, foldLeft, groupBy, and collect. A beginner writes an index loop over 10,000 records, gets an off-by-one error at the boundary, and then adds a mutable accumulator. Fix: describe the transformation, not the steps. For example, keep only even numbers from 1 to 100 and double them; that is a filter followed by a map, not a for loop with var. Aim for expressions under 3-5 lines. If a loop needs more than 5 lines, extract a named function. Run the same example on a 5-item list first; if it works there, it usually works on 10,000 items.

4. Using null and reaching for Option too late

null is allowed for compatibility with Java, but it is not a normal Scala value. Beginners write methods that return null for "not found", then crash 20 lines later when the value is used. Use Option: Some(value) for a result, None for absence. Do not call .get on an Option; use getOrElse, map, fold, or pattern matching. Pattern matching forces you to handle both cases. If you see null in your own new code, replace it before the file reaches 50-100 lines. This is easier than fixing it after 10 call sites. Treat Option as the default for "might be missing", not as an advanced topic for week 6.

5. Learning implicits or givens before the basics

Scala 3 uses given and using; Scala 2 uses implicit. Beginners copy an advanced example in week 1 and spend 2 hours chasing a missing import, a wrong type, or a compiler message that mentions an anonymous value. Leave givens, extension methods, and type classes until you can write a 20-30 line function with pattern matching and a test. You do not need them to filter a list, parse a small file, or model a case class. When you do learn them, learn one example per day, not five. Advanced features are not a badge; they are a tool for removing repetition. If you cannot explain what the compiler is filling in, you are not ready to use it.

6. Treating a failed compile as a mystery

Symptoms: the compiler reports "type mismatch", "not found: value", or a long stack trace after sbt run. Likely causes: you changed one type in one place, imported the wrong version of a library, mixed Scala 2 and Scala 3 syntax, or fixed the second error while the first one caused the rest. The fix: read the first error only, make one change, and compile again. Run sbt clean compile if the build feels stale; give it 5-10 minutes before assuming it is broken. If the same error survives 3 changes, reduce the code to a 5-10 line example and ask for help with that. Do not paste the whole 300-line file into a chat and ask "why".

7. Keeping everything in one file and skipping tests

A beginner builds a 200-300 line Main.scala with input, parsing, calculation, and printing. Then one change breaks three behaviours, and there is no quick way to see which one. Split code when a file passes 100-150 lines: one file for data, one for logic, one for the entry point. Keep the entry point under 20 lines. Write 3-5 small tests for the logic before adding features. A test that runs in under 1 second is better than a manual check you forget. This is not about perfection; it is about making the next mistake easy to find. If you cannot test a function because it prints directly, return a value instead.

FAQ

How can I tell if a Scala tutorial is too advanced?

If it uses given, using, type classes, or implicits before showing basic pattern matching, it is probably too advanced for week 1. If it does not state the Scala version, skip it. A beginner tutorial should fit in 45-60 minutes and produce a runnable example in under 10 minutes.

Should I avoid var completely?

For your first 4-6 weeks, yes, aim for val only. Use var inside a short loop only when the alternative is harder to read. If you cannot explain why the reassignment is needed in one sentence, replace it with a function that returns a new value.

What is the quickest fix for a confusing compile error?

Read the first error, not the last. Change one thing, then compile again; repeat for 5-10 minutes. If the same error survives 3 changes, reduce the code to a 5-10 line example. That usually exposes whether the problem is a wrong type, a missing import, or mixed Scala versions.