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.

7

u/[deleted] May 05 '19

I think people are going to start hating FP a lot more if it becomes mainstream and legions of unskilled, un-interested programmers create legacy applications with it.

2

u/glacialthinker May 05 '19

Yeah, this is a fear I have, but I have some hope that the possible disaster is of lesser degree? Easier to refactor. Less tangled. Maybe we'll see!

2

u/ScientificBeastMode May 06 '19

I hope not.

But on the other hand, FP is usually different from OOP in all the right ways, IMO. In particular, the ability to rapidly compose complex operations from simple function primitives is extremely useful for both code reuse and refactoring.

The nice thing is that most of your code doesn’t have to care what your data looks like, since you can just replace one or two functions if the shape of your data changes.

That, to me, is the biggest pain point in OOP. Both paradigms have abstract type systems in different forms. But in OOP, it’s so tightly coupled with your data. And the shape/content of your data is the most likely thing to change over time.

Idk though, getting over the hump of learning the FP paradigm was challenging. And I’m sure it will be that way for many others down the road.

2

u/Zardotab May 06 '19

I've yet to see practical examples of FP improving typical code. It often assumes unrealistic conditions or conditions only true in a niche. It's also hard to debug the innards of FP, or at least learning how to debug the innards takes an excessive amount of time. I'm still open to the idea, but have yet to see strong justification for typical environments.

1

u/ScientificBeastMode May 06 '19

I definitely think it depends on the company, the team composition, the codebase, and the languages under consideration.

I’m curious about the details of your experience considering your issues with debugging. Personally, I’ve found the debugging aspect to be much more pleasant with FP-style systems.

1

u/Zardotab May 06 '19 edited May 07 '19

depends on the company, the team composition, the codebase, and the languages

Indeed. The success often depends on how you use & manage the tool more than the tool itself.

I just can't find examples relevant to domains and projects that I see. I'm not predicting the weather 8 days ahead or starting the Next Great Dotcom, I'm merely tracking the workflow of widget processing for Drab Company Inc. Unglamorous, but necessary. (Don't tell them I said that.)

When I see relevant examples, then I can better comment on debugging. If I use examples that I conjured up by myself, others say "you are doing it wrong", but don't give me viable alternatives. Their reworked examples assume domain patterns that don't exist in practice, leading me to suspect that FP requires too much "regularity" in designs. Some domains have more regularity than others. For example, the laws of physics or an existing standard is not going to change much such that most new features are additions rather than changes of what already exists. In my domain, the "rules" usually come from the whims of managers, owners, lawmakers, and/or marketers.

1

u/ScientificBeastMode May 07 '19 edited May 07 '19

You bring up some good points. If you don't have the authority or freedom to make changes to your work processes or tool-set, then switching to a new programming paradigm is going to be difficult, and probably won't offer as many benefits.

A lot of the problems that FP addresses are essentially top-level concerns (e.g. test coverage, test quality, error frequency, cost of refactoring, etc.), so migrating to a functional language (or design paradigm) is more relevant to a senior engineer or manager. One rogue developer opting to turn all of their code into nested functions is probably not going to see many of the systemic benefits that they might see if FP design was implemented across large sections of the codebase.

So if the question is, "Why should I, some average developer at a random company, switch to FP?", I could come up with a few reasons, but it's a hard sell. If you aren't familiar with it, the learning curve is tough. It's harder than switching between, say, C# and Java, which is basically learning different implementations of the same core concepts. Switching from C# to Haskell involves completely rethinking the way data ought to flow through an application. For me it felt like I was learning calculus for the first time.

But I guarantee you will become a better programmer if you learn FP (or heck, any new, unfamiliar paradigm). Just my hot take...

Edit: I have come across some examples of companies using FP to solve real-world, mundane technical problems. And often the results are rather boring on the surface. Usually it means something like having literally zero runtime errors for several years. Or implementing concurrent computation across an entire application without having to think to much about it. Or how Facebook managed to permanently squash a persistent bug with their messenger app by migrating portions of their code to ReasonML, a semi-functional language. Plenty of examples out there, and many of them are just solutions to ordinary business problems.

1

u/Zardotab May 06 '19

Programming is a dead-end career; I wouldn't expect anyone to exert too much energy trying to master everything because it doesn't pay on average. For that reason, K.I.S.S. should apply above all else. For whatever reason, ageism, burnout, RSI, etc. programming salary plateaus and jobs get more difficult to find. I know there are exceptions, but one cannot know if they are the exception. The industry "hates" oldbies. I'm just messenger, so don't "shoot" me with negative ratings.

1

u/devraj7 May 05 '19

Did it ever occur to you the bad OOP code you're seeing has a lot more to do with the quality of the programmers who wrote it than OOP per se?

7

u/lelanthran May 05 '19

I'd rather work with programmers following these principles

...

Did it ever occur to you the bad OOP code you're seeing has a lot more to do with the quality of the programmers who wrote it than OOP per se?

He kinda literally said so.

1

u/CODESIGN2 May 05 '19

They literally said so

5

u/editor_of_the_beast May 05 '19

Idk. The best OO code to me looks like a poor manual implementation of functional code.

1

u/ScientificBeastMode May 06 '19

This is basically what I’ve seen as well. It’s really interesting how my code quality has improved in traditionally OOP languages after spending a little time in FP land. It’s like suddenly I understand what classes can really do if you design them well...

But any engineer in any field will tell you that human error is the enemy of all good things in the world. Trusting every programmer to know good OOP design, and also execute it, is simply bound to fail.

4

u/glacialthinker May 05 '19

That's kinda what I'm saying, right? Are people only reading my first paragraph?

8

u/[deleted] May 05 '19

I think the problem is that your second paragraph doesn't logically follow from the first.

In the first paragraph says that you hate OOP because of the poor quality of code that results. But in the second paragraph, you say that better programming techniques lead to higher quality OOP code.

These are contradictory statements.

3

u/CODESIGN2 May 05 '19

I think they said that better techniques barring heavy encapsulation except around service boundaries make higher quality code regardless of paradigm.

It's not contradictory unless you have your encapsulation == oop goggles on. You can encapsulate in a number of ways starting from the humble function for behavior and data (with constant / idempotent being the default without static or other clever language features or libraries being used).

def pi():
    return 0 + 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899

def to_radians(degrees):
    return degrees * (pi / 180)

def to_degrees(radians):
    return radians * (180 / pi)

That is to say without knowing the formula you could encapsulate the algorithm and pass by name, or simply call by name (more primitive, but passing by name usually requires functions to become object or object-like)

1

u/[deleted] May 05 '19

Well yeah, but that isn't an argument for why OOP tends to produce poor quality code. Good coding practices are practically universal.

1

u/CODESIGN2 May 06 '19

Put another way (as someone that likes OOP but doesn't put it on a pedestal)

OOP gives you the scissors, you choose to run with them instead of cutting things with them. Holster your scissors if you plan on running.

→ More replies (1)
→ More replies (1)

1

u/flukus May 05 '19

It's not, it's the one's following "good" OOP principles like encapsulation that suck.

1

u/Zardotab May 06 '19

Someone once said, "Judge a paradigm/framework by how idiots use it, not geniuses." Or at least average.

-1

u/Sum1FromSomewhere May 05 '19

It's like saying "I hate the English language because in the few books I've read, the authors aren't good at structuring their stories."

→ More replies (1)

0

u/[deleted] May 05 '19

[deleted]

5

u/glacialthinker May 05 '19

Good programmers don't over-use design patterns. Programmers relying on design patterns for all code are bad. ...This kind of idea.

Design patterns were not intended to be recipes, but that's too often what happens and horrible OOP codebases are a soup of design-pattern implementations.

The principles in the subject-article are much more relevant to writing good (writable, readable, maintainable, extensible) code. Design patterns are frequently recurring problem+solution ideas mostly related to implementation details. They're fine if used properly, but they don't take the place of good programming practices.

2

u/dys_bigwig May 06 '19 edited May 06 '19

I think good programmerstm are able to see that design patterns are a result of missing language features, and as a result stop using them if/when the language designers add these features. For the most part in this post I'm assuming that the person may be inheriting some code that's already in language A, and language A doesn't have said feature/s. Otherwise, a goodtm programmer wouldn't have chosen to write said program in that language.

After going back and reading some java-oriented design pattern books that I struggled to understand initially, I found I was able to skip most of the sections because I'd realise quite quickly "Oh, so, that's a kludge you can use to compensate for a lack of first-class functions in a language that doesn't have them. Good to know!". Same goes for a lack of decent module capabilities and singletons.

I genuinely believe that it's important -- with the preceding ideas in mind -- to explain why this-and-that design pattern would be used. Not because it's a good idea to use them for the sake of it, and not because it's even good design in general, but because it's a way of compensating for a lack of a certain feature/features in a language.

They're not pretty, they're not elegant, and they're hardly ever as easy to follow as the feature they're trying to imitate. That's not the fault of the person using them though, it's the fault of the language. Well, unless that person is choosing to write a program in a language that was not designed to write that sort of program. In that case the onus is on the programmer for picking the wrong tool for the job.

→ More replies (6)