Comparing FP and OOP
As we have seen in the preceding pages, FP is not exactly a new thing. It, in fact, predates the object-oriented paradigm by a few decades. While Go is multi-paradigm and we can embrace both styles of programming, let’s take a quick look at a concrete comparison between the two.
Functional programming |
Object-oriented programming |
Functions are the bread and butter |
Classes and objects are the bread and butter |
Declarative code |
Imperative code |
Immutability preferred |
Mutable state |
Can enforce purity |
Often no focus on purity |
Recursion |
Loops |
Table 1.1: Table comparing FP (left) and OOP (right)
This comparison is a tad superficial. Many object-oriented languages also have a notion of recursion, but it’s not always central to the language’s design. Similarly, object-oriented code can encapsulate the mutable state and try to get immutability as much as possible.
In today’s world, even languages that we consider traditionally object-oriented, such as Java, are, in fact, becoming more and more multi-paradigm.
As a side note, this comparison might make it seem like there are only three possible paradigms: functional, object-oriented, or multi-paradigm. While these are certainly the most common, there are other paradigms, such as literate programming, logic programming, and reactive programming. As OOP is the main player in this space, and thus what most readers are familiar with, that will be a focus of comparison throughout this book.