I'm a writer, programmer, web developer, and entrepreneur. Preona is my current startup that began its life as the team developing Twitulater. Our goal is to create a set of applications for the emerging Synaptic Web, which would rank real-time information streams in near real time, all along reading its user behaviour and understanding how to intelligently react to it. Swizec is a DZone MVB and is not an employee of DZone and has posted 65 posts at DZone. You can read more from them at their website. View Full User Profile

My Brain Can’t Handle OOP Anymore

05.15.2012
| 17985 views |
  • submit to reddit

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?

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

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

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 at DZone with permission of Swizec Teller, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Steve Brickman replied on Tue, 2012/05/15 - 8:53am

Great, feeling better now, since I'm able to handle the inherent complexity of object-oriented programming with ease :)

Karl Peterbauer replied on Tue, 2012/05/15 - 11:59am

Your example is an intriguing pocket player trick. You ask the wrong question and get the wrong result. 

 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. 

State kept in instance variables is used with a well-defined purpose. Let's rename the class foo to ClickCounter and function add() to addClicks(). Obviously this class might be used to - say - keep track of all clicks in a website.

Now the answer to your question is straight forward, and it makes perfectly sense: The value of a is the number of all clicks after adding a bunch of new ones. The internal state in fact represents the whole execution history of the counter, condensed into the instance variable i. But this is nothing bad or complex, it's the one and only purpose for the counter ;-)

Marko Milicevic replied on Tue, 2012/05/15 - 1:11pm

Functional programming is great for algorithms, but i'm currious how pure functional people handle use cases where OO seems a natural fit (state management and particularly domain modeling)?

 

John J. Franey replied on Tue, 2012/05/15 - 2:14pm

This post seems to take the position that learning functional programming is a bad idea.  It removes the ability to comprehend OO designs/programs, and so would disqualify a person from most software development positions.  Learning functional programming can lead to higher unemployment, and ultimately become responsible for continued economic recession.  Maybe someone in goverment should write a law to bar people from learning functional programming and avoid this catastrophe.

Hernán González replied on Tue, 2012/05/15 - 2:55pm in response to: Karl Peterbauer

Indeed.
Actually, when I read this "argument" against OOP, I started wondering if the article was meant to be some joke. 

 

// What is the value of a? Answering this simple question requires knowing everything.  

 Er... not really. Here's the secret. To get the value of a method calling, you call the method.

Karl Peterbauer replied on Tue, 2012/05/15 - 5:31pm in response to: Marko Milicevic

Just think about a really ubiquitous, indispensable, heavily stateful programming area: Widgets in a GUI toolkit (enabled/disabled, focused/blurred, checked/unchecked, expanded/collapsed, ...), the HTML DOM as a stateful representation of a web page (as opposed to the stateless nature of a low-level "canvas"-API), the JavaFX scene-graph,...

Seb Cha replied on Wed, 2012/05/16 - 4:43am

You named your variables/classes with shitty names like "a", "i", "foo" and "add", and then ask for what they're used for.

You're doing it wrong... ;)

Michael Nitschke replied on Wed, 2012/05/16 - 5:05am

I apologize for sounding like a troll for the next few lines.

The example is horribly over simplified (stripped of all meaning).

And How old are you, if you complain about the observer pattern in your compiler class, get over to the GOF pattern book and you wish you had enough rope to hang your self.

But:

Good OO classes are exactly as you not described them. They have a defined api to other classes, and  they do not make unexpected things in the methods.

If they do, than it's not good code. Go read some books about good oo-style like Clean Code or  Code Complete.

And it helps if you start with Test driven development, it almost forces you to write clean and maintainable code. And you have a good nights rest after you have changed or added functionality (because you know it is still doing what it should do).

I can't stand people whining over things the tried the first time and did not master it completely. You can try chewing bubble gum, this you can master in the first try.

Software development especially anything higher than Basic are like a craft and an art form to learn and to practice. You get better over time, and you only know that you have mastered it, if people come to you to ask you how they could do something or how to learn it.

 Troll off.

Read some more, write some more and stop complaining.

Cheers Mike

Bodo Meyerhorn replied on Wed, 2012/05/16 - 5:37am

I had hoped that DZone would manage to not carry these fashionable articles of "X is dead! Y is the next big thing!" If you have to, by all means write an article about perceived problems and how you would solve them in a different paradigm or language.

 This article is just useless noise.

Paul Russel replied on Sun, 2012/06/10 - 9:18am

The whole point of OOP is that it makes it easier for you to think about the problem and easier to think about how to solve it. If you end up just jamming everything into a big ball of mud you've missed the whole point of OOP (and indeed, programming).

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.