r/programming • u/javinpaul • May 05 '19
10 OOP Design Principles Programmer Should Know
https://hackernoon.com/10-oop-design-principles-every-programmer-should-know-f187436caf655
1
u/andre_2007 May 06 '19
Nice read. Maybe you could also emphasize, although most problems can be solved with OOP, there are cases where other development paradigms fits better and vise versa. For example, the D Programming Language provides multiple development paradigms like OOP, FP, meta programming,... and the developer can choose which paradigm fits best or even combine them to solve a problem.
2
u/javinpaul May 06 '19
Thanks adre_2007, I haven't looked at the D but Scala is the closest one where I have seen the best of both OOP and FP, and it does help.
1
u/andre_2007 May 06 '19
The inventor of Scala was last year guest speaker at the D conference in Berlin. He gave a quite nice presentation. You may come back in future and have a look D, it combines both paradigms and multiple others very well.
3
u/CODESIGN2 May 05 '19
I like the part about coding for interfaces and not implementations. It's harder to do than it sounds. Many interfaces suck because of exactly this. You use push and pop and all of a sudden the backing uses LILO semantics in it's core language, people need to fight the language and their idea-set gets narrowed.
I have to add that it is becoming more and more rare to see people pushing this though.
Imagine you have an average of 4-5 tickets per-day. There is no way those are going to be designed well.
At work we have a thing for storing unstructured data that nobody understands well because it was never documented. As a result everyone hates it. I recently defined a new interface to get at the underlying data and I can see what the author intended. It was quite naive, but also rather beautiful. Any object would extend an interface to be able to store form_data (I think their first mistake, brought about by using ORM). From within that you'd be able to access any object's FormData using a common shared property (weak encapsulation, duck-typing madness).
Put in one or more Interface definitions, they could have added a get_formdata method to a IReadsFormData attribute (where it reads from doesn't matter), and had a set_formdata method on a IWritesFormData methods and it wouldn't matter which property, or even if a property stored a link to all the entries an object could ever have. In-fact it would have freed them entirely from this single table polymorphism BS I keep seeing shat over code-bases.
PS: Anyone that loves data-level polymorphism
your address space is incapable of storing that many records. The more types you add, the less each can store.
why couple storage to business logic? If in future you decided to migrate to a graph store so you can gain insights to how data relates, how will you do that if you pin this directly to some shitty ORM backing? A lot of hard work
How will you manage separation of concerns, neigh separation of domains when the systems naturally mature?
5
u/AlphaDeveloperZA May 05 '19
That's SOLID advice.
-9
u/shevy-ruby May 05 '19
Not really.
Why not?
Because it attempts to tackle different philosophies/usage of/in OOP into "this is the only true OOP way and everything else must be worse". And that is totally rubbish.
Let's use an oft-used Alan Kay quote here:
https://www.quora.com/Why-did-Alan-Kay-dislike-Java
Even with an explanation (though that was not what he may have said origianlly, but it is a good summary nonetheless IMO).
What amazes me is ... people love to critisize Agile. Fair enough, the word itself is a dead-giveaway for marketing. But then they happily use Java and don't understand how there could be anything other than Java for OOP.
And why is DRY even there? Why would this be connected in any way to OOP and nowhere else?
It's logical that, if you can AVOID repeating yourself, that's good. So why is that even mentioned.
It reminds me of other loop-repeated statements such as "use the right tool for the job" (but never specify what this really means) or "learn all the languages" (yeah right because people have an infinite amount of time) or "you will only understand concepts if you use a language which has this concept" (yeah right ... as if any idea is tied to an IMPLEMENTATION ... or does your brain have to ask for some permissions before it can have an idea?).
hackernoon.com is medium.com right? So no surprise ... I dunno why but they always want visitors to log in; probably because the clickety-click leads to monetary flow. Otherwise why are people even using that terrible format for posting their ideas about any subject?
9
5
u/shAdOwArt May 05 '19
Applies equally to all programing paradigms.
Applies equally to all programing paradigms.
Applies equally to all programing paradigms.
Applies equally to all programing paradigms.
Applies equally to all programing paradigms.
How to avoid bad designs that are only possible in OO.
Applies equally to all programing paradigms.
Applies equally to all programing paradigms.
Applies equally to all programing paradigms.
Applies equally to all programing paradigms.
2
u/CODESIGN2 May 05 '19
You can call a function from within another function, then you can compose without OO. What were you thinking? Inheritance could be a specific form of composition if you think about it, you just call the parent function within and set it to a local called parent. You could probably do this using Macro's but then you would be abandoning a lot of engineering
1
u/Zardotab May 06 '19 edited May 06 '19
If you are coding in Java then follow the principle of making variable and methods private by default
I disagree, that creates a lot of verbosity, which slows reading and creates more room for typos. Wait until there is an actual reason or a high likelihood of need. Other languages allow one to switch to accessors under the hood without changing caller code by making a method versus property an interface issue or one in the same (swappable). For example, in "a=x.foo;", "foo" can be a method or attribute. If you start out with "naked" attributes and later need to wrap it, you usually can without reworking code that references "foo". You don't have to "pre-bloat" your code with accessors to achieve that. Java is flawed this way in my opinion, and I hope they fix it. Pre-wrapping everything "just in case" is a bad practice.
The rest of the examples rely too much on shapes, which don't make good examples because in the real world, the subject matter changes more frequently. The rules of geometry are too static to be a good test. I realize they are just training examples, but I've seen people mis-apply lessons learned from shapes into the real world.
And be very careful about splitting domain things into "types". Often that's too blunt of a model. A buffet of features assigned to objects is more flexible. It's similar to the article's Rule 6: "Favor Composition over Inheritance." However, OOP is lousy at set theory in my opinion in order to manage the features as sets. I'd like to see relational modelling extended into code structuring: table oriented programming. OOP is not powerful enough without reinventing a database in it.
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.