Does functional programming replace GoF design patterns?

The blog post you quoted overstates its claim a bit. FP doesn’t eliminate the need for design patterns. The term “design patterns” just isn’t widely used to describe the same thing in FP languages. But they exist. Functional languages have plenty of best practice rules of the form “when you encounter problem X, use code that looks like Y”, which is basically what a design pattern is.

However, it’s correct that most OOP-specific design patterns are pretty much irrelevant in functional languages.

I don’t think it should be particularly controversial to say that design patterns in general only exist to patch up shortcomings in the language.
And if another language can solve the same problem trivially, that other language won’t have need of a design pattern for it. Users of that language may not even be aware that the problem exists, because, well, it’s not a problem in that language.

Here is what the Gang of Four has to say about this issue:

The choice of programming language is important because it influences one’s point of view. Our patterns assume Smalltalk/C++-level language features, and that choice determines what can and cannot be implemented easily. If we assumed procedural languages, we might have included design patterns called “Inheritance”, “Encapsulation,” and “Polymorphism”. Similarly, some of our patterns are supported directly by the less common object-oriented languages. CLOS has multi-methods, for example, which lessen the need for a pattern such as Visitor. In fact, there are enough differences between Smalltalk and C++ to mean that some patterns can be expressed more easily in one language than the other. (See Iterator for example.)

(The above is a quote from the Introduction to the Design Patterns book, page 4, paragraph 3)

The main features of functional
programming include functions as
first-class values, currying,
immutable values, etc. It doesn’t seem
obvious to me that OO design patterns
are approximating any of those
features.

What is the command pattern, if not an approximation of first-class functions? 🙂
In an FP language, you’d simply pass a function as the argument to another function.
In an OOP language, you have to wrap up the function in a class, which you can instantiate and then pass that object to the other function. The effect is the same, but in OOP it’s called a design pattern, and it takes a whole lot more code.
And what is the abstract factory pattern, if not currying? Pass parameters to a function a bit at a time, to configure what kind of value it spits out when you finally call it.

So yes, several GoF design patterns are rendered redundant in FP languages, because more powerful and easier to use alternatives exist.

But of course there are still design patterns which are not solved by FP languages. What is the FP equivalent of a singleton? (Disregarding for a moment that singletons are generally a terrible pattern to use.)

And it works both ways too. As I said, FP has its design patterns too; people just don’t usually think of them as such.

But you may have run across monads. What are they, if not a design pattern for “dealing with global state”? That’s a problem that’s so simple in OOP languages that no equivalent design pattern exists there.

We don’t need a design pattern for “increment a static variable”, or “read from that socket”, because it’s just what you do.

Saying a monad is a design pattern is as absurd as saying the Integers with their usual operations and zero element is a design pattern. No, a monad is a mathematical pattern, not a design pattern.

In (pure) functional languages, side effects and mutable state are impossible, unless you work around it with the monad “design pattern”, or any of the other methods for allowing the same thing.

Additionally, in functional languages
which support OOP (such as F# and
OCaml), it seems obvious to me that
programmers using these languages
would use the same design patterns
found available to every other OOP
language. In fact, right now I use F#
and OCaml everyday, and there are no
striking differences between the
patterns I use in these languages vs
the patterns I use when I write in
Java.

Perhaps because you’re still thinking imperatively? A lot of people, after dealing with imperative languages all their lives, have a hard time giving up on that habit when they try a functional language. (I’ve seen some pretty funny attempts at F#, where literally every function was just a string of ‘let’ statements, basically as if you’d taken a C program, and replaced all semicolons with ‘let’. :))

But another possibility might be that you just haven’t realized that you’re solving problems trivially which would require design patterns in an OOP language.

When you use currying, or pass a function as an argument to another, stop and think about how you’d do that in an OOP language.

Is there any truth to the claim that
functional programming eliminates the
need for OOP design patterns?

Yep. 🙂
When you work in a FP language, you no longer need the OOP-specific design patterns. But you still need some general design patterns, like MVC or other non-OOP specific stuff, and you need a couple of new FP-specific “design patterns” instead. All languages have their shortcomings, and design patterns are usually how we work around them.

Anyway, you may find it interesting to try your hand at “cleaner” FP languages, like ML (my personal favorite, at least for learning purposes), or Haskell, where you don’t have the OOP crutch to fall back on when you’re faced with something new.


As expected, a few people objected to my definition of design patterns as “patching up shortcomings in a language”, so here’s my justification:

As already said, most design patterns are specific to one programming paradigm, or sometimes even one specific language. Often, they solve problems that only exist in that paradigm (see monads for FP, or abstract factories for OOP).

Why doesn’t the abstract factory pattern exist in FP? Because the problem it tries to solve does not exist there.

So, if a problem exists in OOP languages, which does not exist in FP languages, then clearly that is a shortcoming of OOP languages. The problem can be solved, but your language does not do so, but requires a bunch of boilerplate code from you to work around it. Ideally, we’d like our programming language to magically make all problems go away. Any problem that is still there is in principle a shortcoming of the language. 😉

Leave a Comment