Introduction

One of my favorite professional activities is speaking at software conferences. It’s great fun because you get to meet developers who are passionate about their craft, and it gives you as a speaker the opportunity to share knowledge with them.

A talk that I’ve enjoyed giving recently is called “Twins: FP and OOP.” I’ve given it at a number of conferences and user group sessions, and I’ve even had the pleasure of giving it as O’Reilly webcast. Developers enjoy the talk both because it has a large number of references to the film “Twins” and because it discusses one of the age-old relationships between functional and object-oriented programming.

There’s only so much you can say in a conference talk though, so I was really excited when Brian Foster from O’Reilly contacted me to ask if I wanted to expand upon the topic in a report. You can also think of this as a short followup to my earlier O’Reilly published book Java 8 Lambdas (O’Reilly).

You can watch the talk delivered at a conference online or delivered as an O’Reilly webcast.

What Object-Oriented and Functional Programmers Can Learn From Each Other

Before we get into the technical nitty-gritty of lambdas and design patterns, let’s take a look at the technical communities. This will explain why comparing the relationship between functional and object-oriented is so important and relevant.

If you’ve ever read Hacker News, a programming subreddit, or any other online forum, you might have noticed there’s often a touch of friction between functional programmers and developers practicing the object-oriented style. They often sound like they’re talking in a different language to each other, sometimes even going so far as to throw the odd snarky insult around.

On the one hand, functional programmers can often look down on their OO counterparts. Functional programs can be very terse and elegant, packing a lot of behavior into very few lines of code. Functional programmers will make the case that in a multicore world, you need to avoid mutable state in order to scale out your programs, that programming is basically just math, and that now is the time for everyone to think in terms of functions.

Object-oriented programmers will retort that in actual business environments, very few programmers use functional languages. Object-oriented programming scales out well in terms of developers, and as an industry, we know how to do it. While programming can be viewed as a discipline of applied math, software engineering requires us to match technical solutions to business problems. The domain modelling and focus on representing real-world objects that OOP encourages in developers helps narrow that gap.

Of course, these stereotypes are overplaying the difference. Both groups of programmers are employed to solve similar business problems. Both groups are working in the same industry. Are they really so different?

I don’t think so, and I think there’s a lot that we can learn from each other.

What’s in This Report

This report makes the case that a lot of the constructs of good object-oriented design also exist in functional programming. In order to make sure that we’re all on the same page, Chapter 1 explains a little bit about functional programming and the basics of lambda expressions in Java 8.

In Chapter 2, we take a look at the SOLID principles, identified by Robert Martin, and see how they map to functional languages and paradigms. This demonstrates the similarity in terms of higher-level concepts.

In Chapter 3, we look at some behavioral design patterns. Design patterns are commonly used as a vocabulary of shared knowledge amongst object-oriented programmers. They’re also often criticized by functional programmers. Here we’ll look at how some of the most common object-oriented design patterns exist in the functional world.

Most of the examples in this guide are written in the Java programming language. That’s not to say that Java is the only language that could have been used or that it’s even a good one! It is perfectly adequate for this task though and understood by many people. This guide is also motivated by the release of Java 8 and its introduction of lambda expressions to the language. Having said all that, a lot of principles and concepts apply to many other programming languages as well, and I hope that whatever your programming language is, you take something away.

Get Object-Oriented vs. Functional Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.