Object oriented vs. functional programming

by John on November 3, 2010

From Michael Feathers:

OO makes code understandable by encapsulating moving parts.
FP makes code understandable by minimizing moving parts.

This explains some of the tension between object oriented programming and functional programming. The former tries to control state behind object interfaces. The latter tries to minimize state by using pure functions as much as possible.

It’s understandable that programmers accustomed to object oriented programming would like to add functional programming on top of OO, but I believe you have to make more of an exclusive commitment to functional programming to get the most benefit. For example, pure functions are easier to debug and to execute in parallel due to their lack of side effects. But if your code is only semi-functional, you can’t have the same confidence in testing your code or in spreading it across processors.

James Hague argues that 100% functional purity is impractical and that one should aim for 85% purity. But the 15% impurity needs to be partitioned, not randomly scattered across your code base. A simple strategy for doing this is to use functional in the small and OO in the large. Clojure also has some very interesting ideas for isolating the stateful parts of a program.

Related post:

Pure functions have side effects

{ 3 trackbacks }

Tweets that mention Object oriented vs. functional programming — The Endeavour -- Topsy.com
11.03.10 at 11:41
DenkZEIT : Neues Zitat: understanding moving parts in programming paradigms
11.05.10 at 15:18
Where OO has succeeded most — The Endeavour
04.29.11 at 07:17

{ 6 comments… read them below or add one }

1

Paulo Moura 11.05.10 at 14:36

The tension may disappear with a reinterpretation of those OO concepts that make sense in a functional (or, for that matter, logical) context. Interpreting OO as being about controlling state fits an imperative context but there are far more interesting ideas in OO to bring to declarative languages (e.g. code encapsulation and code reuse, as you refer in your other post).

2

Matt 11.14.10 at 16:18

Perhaps that’s why multi-paradigm languages, like C++ or D, are the answer — STL, lambda expressions and function objects are nice examples of mixing the styles that works in practice.

3

Zack Owens 12.22.11 at 10:52

What you’re missing in your analysis is that OO can be implemented in an FP language. Because you have things like first class functions and data structures, you can implement objects with inheritance using method tables. The only differences is that the “this” or “self” object is explicitly passed to the methods. There are plenty of papers out there that show how to implement such a thing.

4

John 12.22.11 at 11:04

In my opinion, inheritance is overrated. In most projects, I wouldn’t miss it if I didn’t have it, especially if I had interfaces (abstract base classes).

5

Ananth Balasubramaniam 12.22.11 at 11:06

While C# is getting more FP bits all the time, the need to keep the language palatable for the “unwashed masses” forces it to make the “popular” choice as opposed to F# for example. I am also becoming increasingly partial to Nemerle (http://nemerle.org) – which is a very powerful language.

6

Jon W 12.22.11 at 22:47

Erlang gets this right! You can isolate dirty code in processes, and because of copy semantics, it can’t undermine any pure process.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>