immutability

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.

Functional programming: Everything is immutable

“Remember how weird it seemed when you first learned that a String in Java was immutable? Well, in functional programming it’s all like that. Everything is immutable.”

(From a discussion with a Java developer recently.)