Uncle Bob about FP vs. OO

 — 1 minute read


Robert C. Martin a.k.a Uncle Bob argues that FP and OO work nicely together and that both concepts are even desirable to play together. For the sake of the argument he reduces object oriented and functional programming to a single core princple each:

So our reductive definition of OO is:

The technique of using dynamic polymorphism to call functions without the source code of the caller depending upon the source code of the callee.

and

So, the reductive definition of functional programming is:

Referential Transparency – no reassignment of values.

He concludes:

It seems clear that the two reductive definitions I have chosen are completely orthogonal. Polymorphism and Referential Transparency have nothing, whatever, to do with each other. There is no intersection between them.

But orthogonality does not imply mutual exclusion (just ask James Clerk Maxwell). It is perfectly possible to build a system that employs both dynamic polymorphism and referential transparency. Not only is it possible, it is desirable!

Why is it desirable? For precisely the same reasons that the two aspects are desirable alone! We desire systems built with dynamic polymorphism because they are strongly decoupled. Dependencies can be inverted across architectural boundaries. They are testable using Mocks and Fakes and other kinds of Test Doubles. Modules can be modified without forcing changes to other modules. This makes such systems much easier to change and improve.

We also desire systems that are built with referential transparency because they are predictable. The inability to change internal state makes systems much easier to understand, to change, and to improve. It drastically reduces the chances of race conditions and other concurrent update problems.

I very much like this and I think languages like for example Scala and Rust have internalized this quite well.