Software for Days

Paradigmatic Advantages of Functional Programming

Excerpted from Functional Programming and the Semantics of Change, State & Time because sometimes 100 words are worth a 1000.

There are many advantages to functional programming. Programs that deal with mutation are “drastically more difficult” to reason about than ones that do not.

Referential transparency is violated when we include set! [i.e. assignment operations] in our computer language. This makes it tricky to determine when we can simplify expressions by substituting equivalent expressions. Consequently, reasoning about programs that use assignment becomes drastically more difficult — SICP Section 3.1.3

In particular, functions avoid temporal coupling since there is no time,

In general, programming with assignment forces us to carefully consider the relative orders of the assignments to make sure that each statement is using the correct version of the variables that have been changed. This issue simply does not arise in functional programs. — SICP Section 3.1.3

and dramatically reduce debugging and unit testing complexity since there is no meaningful context.

“And that is the problem with places. You have this sort of global state that you have to reproduce in order to debug a field problem. That is very very tough.” Rich Hickey, The Value of Values

Reified state couches in writing what must otherwise live in a programmer’s heads — each member of a team can read a function signature instead of building up (hopefully) the same working memory for each object. Couching state in language makes it accessible to static language analysis tools. A compiler may exhaustively permute the set of application states without actually running manually-written (exhausting) unit tests. Functional programs are more easily parallelized since they are just functions with no internal model for time.

Unfortunately, the complexities introduced by assignment become even more problematic in the presence of concurrency. — SICP Section 3.4

These advantages have been known for decades. John Backus “gave high visibility to functional programming” (SICP Section 3.5.5; also see Simon Peyton-Jones, Escape from the ivory tower: the Haskel journey) through his Turing Award lecture in 1978, seven years before the creation of C++ and thirteen before Java. The resistance from an alternative linguistic paradigm perhaps explains the dominance of object-oriented and imperative programming languages, notwithstanding the long-standing advantages of functional programming.