[tweet https://twitter.com/Swizec/status/196543519446409216]

The other week I was working on my compilers homework – the semantic analysis part is an object-oriented nightmare. Something called the visitor pattern to traverse trees and do weird stuff.

It made me want to curl up in a fetal position in the corner, rocking back and forth in a padded room, while mumbling tongues at myself. As River Tam would say before running out of the room in panic “Too crowded!”

What OOP feels like



In object-oriented programming everything gets muddled together. Functions are bundled with data, everything is codependent and there’s no telling what a function might do when you call it.

And that’s assuming there are no side-effects outside the object monad. Otherwise, who knows, the world might explode!

This creates software that is impossible to understand. I used to think I could, but after a few months of functional-style programming I realized that I simply don’t have the brainpower to understand object oriented code. It’s too messy.

When you put singletons into the picture and objects using other objects it all just gets … let me give you a simple example:

foo: object {
   i: private integer = 0;
   add: function (a:integer) {
      i += a;
      return i;
/* lots of code happens here, foo has been passed around, things happened */
/* foo is not a singleton though, just used a lot */
a:integer = foo.add(5);
// What is the value of a?

Answering this simple question requires knowing everything. The whole execution history of foo. The whole codebase. You name it, you have to keep it in your head.

Oh and did I mention class bar? It depends on foo for a lot of its stuff. Oh yeah, they got into a little friends with benefits situation last year. It all gets pretty interesting. I think bar cheated on foo with baz once, though. There’s no telling how foo might react to that!

Functional programming

fp, by okeef creations

fp, by okeef creations

With a lot of gentle poking and prodding from @sbelak, I started learning functional programming about two years ago.

I was promised easy scalability, working with multiple processors without cost, expressive code, things that Just Work ™ and software with less bugs that is easier to maintain and shorter/quicker to write.

I may not have gotten all that, but I did get a whole new way of thinking about my craft. An easier way to understand.

At first functional programming felt like performing a lobotomy on myself by sticking hot pokers into my feet. Are you sure I don’t need loops? But I do need variables don’t I? At least a bit of mutability? Really? I don’t? This is some sort of hazing ritual isn’t it, any moment now you’re all going to jump up, shout “surprise” and start laughing at me.

 But I kept at it. And even though my code looked horrible, I fell in love.

The first attraction of functinal style code was writing python functions that just pass data around. No storing in intermediate variables, just passing one function’s result directly into the arguments of the next function in the chain.

Because who cares about reading intermediate steps? All I care for is that this chunk of code creates X. Not that it first does Y, then Z, then pulls in A and B and combines them into X.

Sure, you could define a function called X that combines Y, Z, A and B, but why create a new function for something that only gets called once? Much better to just do something like X = A+B(Y(Z)) isn’t it?

A waterfall of data

Plitvice falls

Plitvice falls

The way I think about code now is a waterfall of data.

You have data and every function in the chain is a ledge. The data stops falling, changes course a bit, perhaps even changes some properties, and falls onward to the next function.

In the end you have a pool. This is where your data ends up after falling through many functions and being changed. But then it flows onward to the next function, or wherever you might need it.

You might say this is really procedural programming, but I don’t like side-effects, imperative bits of code and so on. It’s still functional programming, but this is how my brain understands code. Your mileage might vary.


Whatever way you think about your code, whatever way you visualise execution in your head … hat’s off to you, if you can handle the inherent complexity of object-oriented programming. You are a better man than I am.

I think you should give “this functional programming fad” a try anyway. You  might like it 😉

Enhanced by Zemanta

You should follow me on twitter, here.