Swizec Teller - a geek with a hatswizec.com

Senior Mindset Book

Get promoted, earn a bigger salary, work for top companies

Senior Engineer Mindset cover
Learn more

    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 ;)

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

    Did you enjoy this article?

    Continue reading about My brain can't handle OOP anymore

    Semantically similar articles hand-picked by GPT-4

    Senior Mindset Book

    Get promoted, earn a bigger salary, work for top companies

    Learn more

    Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.

    Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.

    Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.

    Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev

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

    Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections

    Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog

    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 ❤️

    Created by Swizec with ❤️