Skip to content
Swizec Teller - a geek with a hatswizec.com

My brain can't handle OOP anymore

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

Schizophrenic?

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

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 (tm) 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

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.

Fin

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

Did you enjoy this article?

Published on May 7th, 2012 in Data, Firefly, Foobar, Function, Functional programming, Joss Whedon, Languages, Object-oriented programming, Programming, River Tam, Uncategorized, Visitor pattern

Learned something new?
Want to become a high value JavaScript expert?

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

Start with an interactive cheatsheet 📖

Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their JS careers with my letters, workshops, courses, and talks. ✌️

Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️