r/coding • u/javinpaul • May 08 '19
10 OOP Design Principles to write SOLID Code
https://hackernoon.com/10-oop-design-principles-every-programmer-should-know-f187436caf65
0
Upvotes
4
u/_Pho_ May 08 '19
"10 design principles for these 5 design principles"
Edit: after reading it yeah this is a garbage article
0
3
u/Rainfly_X May 08 '19
First of all, completely agree with the other critics of the article. I do actually see it even worse, as someone who's become a critic of (conventional, at least) OOP as I've grown as a programmer. A lot of these suggestions may have some value, but as blind maxims will inevitably lead to worse code.
DRY: A lot of bad code is bad because it's repetitious, especially in dangerous ways. But I've also written code that was unmaintainable because it tried too hard to share code and shave characters, which made things so interconnected that they couldn't safely be changed. DRY is good, it is not gospel - it should defer to clarity and flexibility, in the event of a conflict.
Encapsulate What Changes: Unless you have amazing domain knowledge or the freaking Time Stone, you don't know what will change. You will predict wrong, and structure your project in cumbersome ways. YAGNI will serve you better.
Open-Closed Principle: This is just explained poorly. The example is just tight coupling. Even when explained well, this principle has traditionally been used to justify inheritance features like information hiding and protected fields, which are basically bad and superseded by composition.
SRP is a nice principle, but doesn't take you very far. Divvying up responsibilities is a subjective process. Green coders will often wonder, "is this a responsibility?" and feel like failures for being lost. This is inherently subjective, even flexible over time, and SRP offers basically no guiding light into that subjectivity.
Dependency Injection: Every implementation I've ever seen has introduced issues of complexity, actually made testing harder, and handled it poorly if you have multiple injectables of the same type. You can do the same stuff with compile-time interfaces and parameters, and it'll be easier to follow.
Composition: Best advice of the whole article, but conflicts with old-fashioned (worse) OOP like #3 in the very same article. It also conflicts with...
LSP: This is so specific to inheritance, right after the Bart Simpson image about not using inheritance. What's worse, is that interfaces are vastly simpler from a consumer perspective. But then I suppose you don't need a PluralSight discount.
ISP: This is just the SRP in a different costume. Sorry not sorry.
Program for Interface: This is TERRIBLE advice if you aren't writing against a tried-and-true interface. Conversely, it's a great way to lock yourself into a bad interface, that sounded really great before you knew the implementation constraints. I've found that top-down and bottom-up programming both have advantages, but the latter is better for exploratory work, and the former is good for taking a mess of bottom-up pieces and shaping them up into a cohesive program.
Delegation: Poorly explained. It's also very situationally useful. I find it's usually more valuable to spend extra time on the quality of my plain old data structures so that good behaviors arise naturally (being able to compare with built-in/bytewise equality, etc.). The whole question of where to put functionality, also becomes a lot simpler if you don't have to pick "which object do I glue this function to", if the function can just live on its own and take an interface value/pattern match. You can't do this in Java because it's drowning in OOP flavor-aid, where every verb must be owned by a noun. This is part of why, as much as I try to write idiomatically in whatever language I'm using, I broadly prefer functional or data-oriented approaches to problems. OOP is frankly... bad, at least IMHO.
I've already spent 10X the effort on this post as the OP did. But it was worthwhile, to me if nobody else, as a mental exercise. If it was just about rustled jimmies, that wouldn't be very fun, but using rustled jimmies as an excuse to really examine the popular tropes of OOP in the lens of my (relatively recent) changes in perspective? That's fun.