idiom

Scala idiom: Prefer immutable code (immutable data structures)

(I originally wrote this blog post in 2012, and it seems like it has held up well over time.)

One great thing I’ve learned from Scala and functional programming over the last few months is this: 

Make your variables immutable, unless there’s a good reason not to.

“Immutable” means that you can’t change (mutate) your variables; you mark them as final in Java, or use the val keyword in Scala. More important than you not changing your variables is that other programmers can’t change your variables, and you can’t change theirs.

Scala best practice: Eliminate null values from your code

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.5, “Scala best practice: Eliminate null values from your code.”

Problem

Tony Hoare, inventor of the null reference way back in 1965, refers to the creation of the null value as his “billion dollar mistake.” In keeping with modern best practices, you want to eliminate null values from your code.

Scala best practice: Use match expressions and pattern matching

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.4, “Scala best practice: Use match expressions and pattern matching.”

Back to top

Problem

Match expressions (and pattern matching) are a major feature of the Scala programming language, and you want to see examples of the many ways to use them.

Back to top

Solution

Match expressions (match/case statements) and pattern matching are a major feature of the Scala language. If you’re coming to Scala from Java, the most obvious uses are:

Table of Contents

  1. Problem
  2. Solution
Back to top

When to use an abstract class in Scala (instead of a trait)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.12, “When to use an abstract class in Scala.”

Problem

Scala has traits, and a trait is more flexible than an abstract class, so you wonder, “When should I use an abstract class?”

Solution

There are two main reasons to use an abstract class in Scala:

Make the Scala Vector class your default immutable sequence

This is an excerpt from the Scala Cookbook. This is Recipe 10.7, “Make the Vector Class Your ‘Go To’ Immutable Sequence.”

Problem

You want a fast, general-purpose, immutable, sequential collection type for your Scala applications.

Solution

The Vector class was introduced in Scala 2.8 and is now considered to be the “go to,” general-purpose immutable sequential data structure.

Scala: Understanding mutable variables with immutable collections

This is an excerpt from the Scala Cookbook (partially re-worded for the internet). This is Recipe 10.6, “Understanding Mutable Variables with Immutable Collections.”

Problem

You may have seen that mixing a mutable variable (var) with an immutable collection causes surprising behavior. For instance, when you create an immutable Vector as a var, it appears you can somehow add new elements to it:

Initialize Scala variables with Option, None, and Some (not null)

Summary: How to properly use the Scala Option/Some/None idiom to initialize empty var fields -- and specifically how not to use null values for the same purpose.

When you get started in the Scala world, you quickly learn that null values are a bad thing. Scala makes it easy to replace null values with something better, and that something better is what I call the Option/Some/None pattern (or idiom).

Scala FP idiom: Methods should not have side effects

An functional programming idiom, and therefore a Scala idiom, is that functions and methods should not have side effects. As written in the book Programming in Scala:

A method's only act should be to compute and return a variable.