Functional vs.? Object Oriented Programming
On the difference between functional and object oriented programming, how they’re actually the same thing and why you should use both.
— — — — — — — — — — — — — — —
THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.
— — — — — — — — — — — — — — —

Tags: #FYI++
This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.
It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.
In the following articles, I want to go through the typical (and not so typical) functions which are used to transform collections — map, filter, groupBy etc. Often, I have been confronted with the view that using these functions amounts to functional programming. I’m not sure I agree with that view, similar to how I’m not sure I would agree that changing tires constitutes being an auto-mechanic.
However, since it is at least possible that many readers will share this preconception, I will take advantage of the opportunity to give some general thoughts on functional programming which might be useful to newcomers, and help them incorporate the term into their mental frameworks.
FP vs. OOP?
It is often the case that the techniques of FP (Functional programming) and OOP (Object-oriented programming) are viewed as mutually exclusive — that you somehow have to use one or the other. People from the OOP camp view FP as a kind of black magic filled with big bad words like functors and monads, while people from the FP camp view OOP as the easy way for the stupid, primitive masses which are completely ignorant about “the higher way”. Since OOP has been in the mainstream for a longer time, the OOP camp is larger. It is interesting to realize that, for the same reason, most of the people in the FP camp started in the OOP camp — rarely is the opposite true.
However, I would like to present a different view. I think that this battle between camps is nonsensical, because the two are not mutually exclusive. In fact, not only can you use both FP and OOP, you absolutely should! The reason is this: in a sense, OOP and FP are both a consequence of the same fundamental design techniques, just applied to different design problems.
The design techniques we are talking about are ones that you apply every day, without thinking:
- Decomposition: When building a big thing, decompose it into comprehensive smaller things
- Abstraction: When you encounter a repeating pattern, create an abstraction that expresses it
So, let’s use these techniques to tackle the following problem:
Design an application that exposes an API to accept some type of data (a text file, a string, a list of strings, …), performs some type of analysis (count words, semantic analysis, whatever), and saves the result to a database.
The exact details are not important for our discussion.
OOP
Immediately, even as you were reading the sentence above, you were already decomposing the application into smaller components — there’s going to be a controller, a DBO, a service layer, etc. If you didn’t use a framework and wrote the whole thing from scratch, you would certainly encounter many patterns that you would express using an abstraction — repeated code that defines endpoints, processes requests/responses etc. (a HTTPController), code that validates the different kinds of input (a Validator), etc.
All those things, those objects, have behaviors that are useful to the problem we're trying to solve. Of course, this is exactly what OOP is about, but what we want to emphasize is that designing things in an object-oriented manner is really a consequence of the two techniques we talked about (decomposition, abstraction), along with the fact that thinking in terms of objects and behaviors is something that we as humans feel comfortable with.
Now, if we were hardcore members of the OOP camp, the designing phase is likely over. We’ve decomposed the problem into a bunch of objects, so now it’s time to roll up our sleeves and go build them. And for the most part, that means writing methods. So we do that, writing method after method, building each method by implementing its atomic steps using lots of for-cycles and ifs and assignments and stuff.
FP
Here’s my question: why? Instead of approaching those methods as a design problem and using the same techniques we just talked about, the same techniques that we use completely automatically when designing things, we just start ‘hacking away’. No looking for patterns, no decomposition into comprehensive units. For some reason, we tend to approach the problem of building behaviors (as opposed to things) in a completely ad hoc manner.
‘Hey, maybe we should start doing this’ is basically the core idea of FP — just use the same techniques you already do, but apply them when designing behaviors. That’s it, nothing special about it.
When you start doing that, you will notice a lot of repeating patterns, and that’s where map and fold and all the other functions come from — they are names for units of code that are useful when building behaviors. Every single one of those units can (and is) implemented using a combinations of ifs and for-loops, but contrary to ifs and for-loops, they have a name which communicates what they do.
Simple FP example
Take a look at the following:

