I decided to watch some programming-related videos and I began with uncle Bob’s keynote The Last Programming Language. In general I agree with what he says, with one exception. Uncle Bob believes that functional programming is important because it plays well with multicore programming.
I don’t think that this is the most important value of functional programming. First of all, there’s a lot of code written out there that is not multicore-aware. That can happen for example when single core performance is sufficient. You can laugh as much as you want about this statement but there are many programs where it is actually very true.
On top of that, there are many non functional programming languages that can support multicore programming without necessarily using threads. For example C++ code can use Open MPI, and Python code can use the multiprocessing module.
So what’s the real added-value of functional programming? From what I have seen so far and without being an expert I see that functional code is:
- Shorter because it uses higher-order functions that can generally be applied to all (uniform) collections/data structures
- Easier to test because functions use immutable code and are side-effect free
- Easier to read because it avoids branching
- In general easier to reason about
Of course we need to be careful about point 3, especially when we talk about parentheses in Lisp code
or code like the Combinatory Haskell programmer (disclaimer: I have never coded in Haskell)
s f g x = f x (g x)
k x y = x
b f g x = f (g x)
c f g x = f x g
y f = f (y f)
cond p f g x = if p x then f x else g x
fac = y (b (cond ((==) 0) (k 1)) (b (s (*)) (c b pred)))
But in general I believe that with good coding conventions functional code can be readable and easy to understand.