r/programming • • May 05 '19

10 OOP Design Principles Programmer Should Know

https://hackernoon.com/10-oop-design-principles-every-programmer-should-know-f187436caf65
2 Upvotes

44 comments sorted by

View all comments

25

u/glacialthinker May 05 '19 edited May 05 '19

I tend to hate OOP because of the poor quality of code which results: 10% of the code being relevant to the problem, the rest being boilerplate, excessive interface and encapsulation fluff (which also has to change as the program evolves anyway), redundancy because of object hierarchies tied to types/data or functionality hidden by abstraction, and more...

However, this set of principles (the article) can lead to much better object-oriented code, and most of them can apply to programming in general. I'd rather work with programmers following these principles (though reserving heavy encapsulation for library or external interfaces) than working with a tool-belt full of design-patterns.

-2

u/pron98 May 05 '19 edited May 05 '19

10% of the code being relevant to the problem

I would challenge that number. People who tend to bring this up also tend to greatly exaggerate differences in LOC between different languages/styles. While boilerplate may end up taking up a non-negligible portion of a small program, it is not all that much in large programs (and those are the ones that matter, because small programs are easy to understand, anyway). When we look at large programs we see a similar number of LOC regardless of language/style (of course when comparing high-level languages). You may see a 1.2x difference, or maybe 1.5x difference in some cases, but you don't see a 3x or a 5x difference, let alone 10x.

My hypothesis for why some people tend to so grossly overestimate boilerplate or other kinds of "accidental complexity" in current high-level languages is simply because however much there is, they find it annoying, and when we do an annoying task we exaggerate its scope.

1

u/glacialthinker May 06 '19

Your reply made me reflect a bit on why I said 10%. It seems I have three codebases in mind which are fuel for this: OO gameplay code from almost 20 years ago, most of the 1mil lines of CryEngine, and the Oculus SDK. I guess these scarred me. However, these are also the codebases heaviest on object orientation and design patterns which I've worked with (and CryEngine isn't all that OO... just enough to kludge it up, though several coworkers did their best to add even more "good OO practices" so that added to my frustration).

The reality is that refactoring could slim the code tremendously in all of these cases, though it would likely be more of a rewrite because of the amount of abstractions involved -- and this is part of my gripe. Abstractions should make this easier. In practice, these out of control design-patterns and encapsulation-heavy codebases are rigid and unless interfaces are very carefully designed, they also need to change -- often many layers worth. It's like "Oh I forgot the stuffing in the chicken in the turducken -- let's cut through all those layers and try to fix this and sew it all up."

Anyway, due to your reply I was thinking "yeah, I'm probably exaggerating", but no I can't say I was. And we do have examples of codebases shrinking by 5-10x when they are rewritten. It doesn't have to be a language thing, but my issue is that object-oriented style seems prone to getting out of control. That's just been my experience. Even classic procedural C-style code is just simpler, with a higher density of code relevant to the problem rather than filled with unnecessary (or even self-defeating) abstractions.

1

u/pron98 May 06 '19 edited May 06 '19

And we do have examples of codebases shrinking by 5-10x when they are rewritten.

That's true. The reason is that code is written organically. You're not sure what exactly you'll need, you don't fully understand the problem, etc..

It doesn't have to be a language thing, but my issue is that object-oriented style seems prone to getting out of control.

I have a feeling, though no data to support it, that the more clever the language, the more distracting it is from the domain, drawing programmers -- especially younger programmers -- to use clever features. Of course, it may have its advantages, but it could also lead to YAGNI. The C++ codebase I'm working on now (HotSpot) is over 25 years old, but I don't see this problem there at all. Neither did I see it in the other codebases I personally worked on, but I have usually worked on codebases written by very experienced developers. HotSpot, in particular, is heavily maintained and constantly refactored; just the other day someone deleted many thousands of lines in a refactor. We try to write the simplest code possible, and only go back and add abstractions if we see that a piece of code requires constant changes to support new features. But when that happens -- and it happens often, just not everywhere -- we appreciate that we're not using C. I agree there is a tension between using a language with more powerful abstractions, and a desire to use that power even when unnecessary, but I also hear many complaints about the opposite problem, where codebases written in very unpowered languages (e.g. Go) that the lack of abstractions also leads to a lot of waste.