---
created_at: '2015-05-04T05:27:50.000Z'
title: Functional Programming Doesn't Work (and what to do about it) (2009)
url: http://prog21.dadgum.com/54.html
author: tosh
points: 43
story_text: ''
comment_text: 
num_comments: 54
story_id: 
story_title: 
story_url: 
parent_id: 
created_at_i: 1430717270
_tags:
- story
- author_tosh
- story_9484470
objectID: '9484470'

---
[Source](http://prog21.dadgum.com/54.html "Permalink to Functional Programming Doesn't Work (and what to do about it)")

# Functional Programming Doesn't Work (and what to do about it)

[programming in the  
twenty-first century][1]

It's not about technology for its own sake. It's about being able to implement your ideas.

# Functional Programming Doesn't Work (and what to do about it)

Read suddenly and in isolation, this may be easy to misinterpret, so I suggest first reading some past articles which have led to this point:

* [Admitting that Functional Programming Can Be Awkward][2]
* [Follow-up to "Admitting that Functional Programming Can Be Awkward"][3]
* [Back to the Basics of Functional Programming][4]
* [Purely Functional Retrogames][5]
* [Puzzle Languages][6]
* [How I Learned to Stop Worrying and Love Erlang's Process Dictionary][7]

After spending a long time in the functional programming world, and using Erlang as my go-to language for tricky problems, I've finally concluded that purely functional programming isn't worth it. It's not a failure because of soft issues such as marketing, but because the further you go down the purely functional road the more mental overhead is involved in writing complex programs. That sounds like a description of programming in general--problems get much more difficult to solve as they increase in scope--but it's much lower-level and specific than that. The kicker is that what's often a tremendous puzzle in Erlang (or Haskell) turns into straightforward code in Python or Perl or even C.

Imagine you've implemented a large program in a purely functional way. All the data is properly threaded in and out of functions, and there are no truly destructive updates to speak of. Now pick the two lowest-level and most isolated functions in the entire codebase. They're used all over the place, but are never called from the same modules. Now make these dependent on each other: function A behaves differently depending on the number of times function B has been called and vice-versa.

In C, this is easy! It can be done quickly and cleanly by adding some global variables. In purely functional code, this is somewhere between a major rearchitecting of the data flow and hopeless.

A second example: It's a common compilation technique for C and other imperative languages to convert programs to single-assignment form. That is, where variables are initialized and never changed. It's easy to mechanically convert a series of destructive updates into what's essentially pure code. Here's a simple statement:
    
    
    if (a > 0) {
       a++;
    }

In single-assignment form a new variable is introduced to avoid modifying an existing variable, and the result is rather Erlangy:
    
    
    if (a > 0) {
       a1 = a + 1;
    } else {
       a1 = a;
    }

The latter is cleaner in that you _know_ variables won't change. They're not variables at all, but names for values. But _writing_ the latter directly can be awkward. Depending on where you are in the code, the current value of whatever "a" represents has different names. Inserting a statement in the middle requires inventing new names for things, and you need to make sure you're referencing the right version. (There's more room for error now: you don't just say "a," but the name of the value you want in the current chain of calculations.)

In both of these examples imperative code is actually an optimization of the functional code. You could pass a global state in and out of every function in your program, but why not make that implicit? You could go through the pain of trying to write in single-assignment form directly, but as there's a mechanical translation from one to the other, why not use the form that's easier to write in?

At this point I should make it clear: functional programming is useful and important. Remember, it was developed as a way to make code easier to reason about and to avoid "spaghetti memory updates." The line between "imperative" and "functional" is blurry. If a Haskell program contains a BASIC-like domain specific language which is also written in Haskell, is the overall program functional or imperative? Does it matter?

For me, what has worked out is to go down the purely functional path as much as possible, but fall back on imperative techniques when too much code pressure has built up. Some cases of this are well-known and accepted, such as random number generation (where the seed is modified behind the scenes), and most any kind of I/O (where the position in the file is managed for you).

Learning how to find similar pressure relief valves in your own code takes practice.

One bit of advice I can offer is that going for the obvious solution of moving core data structures from functional to imperative code may not be the best approach. In the Pac-Man example from [Purely Functional Retrogames][5], it's completely doable to write that old game in a purely functional style. The dependencies can be worked out; the data flow isn't really that bad. It still may be a messy endeavor, with lots of little bits of data to keep track of, and selectively moving parts out of the purely functional world will result in more manageable code. Now the obvious target is either the state of Pac-Man himself or the ghosts, but those are part of the core data flow of the program. Make those globally accessible and modifiable and all of a sudden a large part of the code has shifted from functional to imperative...and that wasn't the goal.

A better approach is to look for small, stateful, bits of data that get used in a variety of places, not just on the main data flow path. A good candidate in this example is the current game time (a.k.a. the number of elapsed frames). There's a clear precedent that time/date functions, such as Erlang's `now()`, cover up a bit of state, and that's what makes them useful. Another possibility is the score. It's a simple value that gets updated in a variety of situations. Making it a true global counter removes a whole layer of data threading, and it's simple: just have a function to add to the score counter and another function to retrieve the current value. No reason to add extra complexity just to dodge having a single global variable, something that a C / Python / Lua / Ruby programmer wouldn't even blink at.

(Also see the [follow-up][8].)

[Russian][9]   
[Indonesian][10]   
[Portugese][11]

[permalink][12] _December 28, 2009_

# previously

* [How I Learned to Stop Worrying and Love Erlang's Process Dictionary][13]
* [Slow Languages Battle Across Time][14]
* [How Did Things Ever Get This Good?][15]
* [Tales of a Former Disassembly Addict][16]
* [Micro-Build Systems and the Death of a Prominent DSL][17]

# [archives][18]

[**twitter][19]** / [**mail][20]**

I'm James Hague, a [recovering programmer][21] who has been designing video games since the 1980s. [Programming Without Being Obsessed With Programming][22] and [Organizational Skills Beat Algorithmic Wizardry][23] are good starting points. For the older stuff, try the [2012 Retrospective][24].

Where are [the comments][25]?

[1]: http://prog21.dadgum.com/
[2]: http://prog21.dadgum.com/3.html
[3]: http://prog21.dadgum.com/4.html
[4]: http://prog21.dadgum.com/18.html
[5]: http://prog21.dadgum.com/23.html
[6]: http://prog21.dadgum.com/38.html
[7]: http://prog21.dadgum.com/53.html
[8]: http://prog21.dadgum.com/55.html
[9]: http://softdroid.net/funkcionalnoe-programmirovanie-ne-rabotaet
[10]: https://www.chameleonjohn.com/translations/54-Indonesian
[11]: http://www.travel-ticker.com/translations/functional-programming-54
[12]: http://prog21.dadgum.com/54.html
[13]: 53.html
[14]: 52.html
[15]: 51.html
[16]: 50.html
[17]: 49.html
[18]: http://prog21.dadgum.com/archives.html
[19]: https://twitter.com/dadgumjames
[20]: mailto:james.hague%40gmail.com
[21]: http://prog21.dadgum.com/56.html
[22]: http://prog21.dadgum.com/195.html
[23]: http://prog21.dadgum.com/177.html
[24]: http://prog21.dadgum.com/162.html
[25]: http://prog21.dadgum.com/57.html

