Doing some research for an upcoming installment of an infamous series of blog posts (to be released at an undetermined date) I’ve come to notice this annoyance. In general I like object-oriented programming. I think it allows you to take a complicated problem and produce simple, more reusable and easier to maintain code. Assuming the programmer is sane individual, and that sometimes is a leap of faith.
I’m not sure if there are programmers who feel the need to complicate things just for the sake of showing off (oh, look at me!), or if they legitimately don’t know any other way. I suspect a little of both. Perhaps the programmer thought it was possible the code would grow in complexity in the future, and just wanted to prepare in advance. I don’t know. But it annoys me.
I consider this abuse of Object-oriented programming to be Object-oriented masturbation since the only one who gets any enjoyment out of it is the developer who does it. Here’s a slightly exaggerated (though not far off) example of Object-oriented masturbation typical of what I’ve seen many times before:
Sum two numbers and print the result in the format “The answer is: X” where X is the sum of the two numbers.
Correct (sane) Answer
Arguably, you could do the math and store in a variable if you are squeamish to doing math in any line containing an output method. Regardless, this is dead obvious and simple. And yes, this technically still does use OOP since it uses
Insane (masturbatory) Answer
I did the exact same thing in 1 logical line of code (3 if you include the function itself) up above. What was gained from the object-oriented design? I argue nothing. Rewriting the first example would take a minute should something need changing. If there were a need to localize that string, I could pull it out and either make that a 3rd parameter, or some global string the function can use. There’s no benefit here. This is over engineering. There’s nothing truly reusable here. The closest is the
Printer.out() method, and in reality it’s a wrapper for
The bonus joke is that the object-oriented implementation is significantly slower.
Run 1: 0.0266, 0.0451 Run 2: 0.0314, 0.0464 Run 3: 0.0329, 0.0462 Run 4: 0.0268, 0.0468 Run 5: 0.0274, 0.0475 Avg: 0.02902, 0.0464
The first example is also 62.5% faster in Firefox 3.0.4 since the all that overhead is gone. If you have FireBug installed you can check that out here.
ob-jikt | o·ri·ent-ed | mas⋅tur⋅ba⋅tion
- the stimulation or manipulation of one’s own ego by way of using object-oriented code in places where it has no advantage instead resulting in unnecessary complication and bloat.
If your guilty of doing this, please cut it out. It’s annoying. Thank you.
No I will not single out links to the pages that inspired this, but rest assured it litters the web.