mutate

This is a page from my book, Functional Programming, Simplified

Scala/FP Idiom: Update as You Copy, Don’t Mutate

“I’ve been imitated so well I’ve heard people copy my mistakes.”

Jimi Hendrix

Goals

In functional programming you don’t modify (mutate) existing objects, you create new objects with updated fields based on existing objects. For instance, last year my niece’s name was “Emily Maness,” so I could have created a Person instance to represent her, like this:

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.