side effects

FP is not about not having side effects at all

“I though it was obvious, but apparently it's not. FP is not about not having side effects at all, otherwise it would be useless. It's just about deferring them for as long as possible, that's all.”

~ Alessandro Lacava, in this tweet

Scala style: Side-effect methods with no parameters should be declared with parentheses

As a note about Scala style, this Scala page strongly encourages that side-effect methods that takes no parameters should be declared with parentheses:

“However, this syntax (leaving off parentheses) should only be used when the method in question has no side-effects (purely-functional). In other words, it would be acceptable to omit parentheses when calling queue.size, but not when calling println() (or openGarageDoor()). This convention mirrors the method declaration convention given above.”

“Religiously observing this convention will dramatically improve code readability and will make it much easier to understand at a glance the most basic operation of any given method. Resist the urge to omit parentheses simply to save two characters!”

Scala best practice: Create methods that have no side effects (pure functions)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.1, “Scala best practice: Create methods that have no side effects (pure functions).”


In keeping with the best practices of Functional Programming (FP), you want to write “pure functions.”

A guide rope for functional programming (Mary Rose Cook)

Mary Rose Cook has written a great, simple introduction to functional programming. The image shows the introduction to her article. I find it interesting to think of certain aspects of functional programming as writing pipelines, as in Unix/Linux pipeline commands. If your methods/functions have no side effects, you can write pipelines in your functional code.

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.