if you have the time, a really good talk on the subject and history.
Programming
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities !webdev@programming.dev
One example is creating an interface for every goddamn class I make because of “loose coupling” when in reality none of these classes are ever going to have an alternative implementation.
Sounds like you've learned the answer!
Virtual all programming principles like that should never be applied blindly in all situations. You basically need to develop taste through experience... and caring about code quality (lots of people have experience but don't give a shit what they're excreting).
Stuff like DRY and SOLID are guidelines not rules.
The principles are perfectly fine. It's the mindless following of them that's the problem.
Your take is the same take I see with every new generation of software engineers discovering that things like principles, patterns and ideas have nuance to them. Who when they see someone applying a particular pattern without nuance think that is what the pattern means.
I'm making a separate comment for this, but people saying "Liskov substitution principle" instead of "Behavioral subtyping" generally seem more interested in finding a set of rules to follow rather than exploring what makes those rules useful. (Context, the L in solid is "Liskov substitution principle.") Barbra Liskov herself has said that the proper name for it would be behavioral subtyping.
In an interview in 2016, Liskov herself explains that what she presented in her keynote address was an "informal rule", that Jeannette Wing later proposed that they "try to figure out precisely what this means", which led to their joint publication [A behavioral notion of subtyping], and indeed that "technically, it's called behavioral subtyping".[5] During the interview, she does not use substitution terminology to discuss the concepts.
You can watch the video interview here. It's less than five minutes. https://youtu.be/-Z-17h3jG0A
YAGNI ("you aren't/ain't gonna need it) is my response to making an interface for every single class. If and when we need one, we can extract an interface out. An exception to this is if I'm writing code that another team will use (as opposed to a web API) but like 99% of code I write only my team ever uses and doesn't have any down stream dependencies.
99% of code is too complicated for what it does because of principles like SOLID, and because of OOP.
Algorithms can be complex, but the way a system is put together should never be complicated. Computers are incredibly stupid, and will always perform better on linear code that batches similar operations together, which is not so coincidentally also what we understand best.
Our main issue in this industry is not premature optimisation anymore, but premature and excessive abstraction.
This is crazy misattribution.
99% of code is too complicated because of inexperienced programmers making it too complicated. Not because of the principles that they mislabel and misunderstood.
Just because I forcefully and incorrectly apply a particular pattern to a problem it is not suited to solve for doesn't mean the pattern is the problem. In this case, I, the developer, am the problem.
Everything has nuance and you should only use in your project the things that make sense for the problems you face.
Crowbaring a solution to a problem a project isn't dealing with into that project is going to lead to pain
why this isn't a predictable outcome baffles me. And why attribution for the problem goes to the pattern that was misapplied baffles me even further.
My somewhat hot take is that design patterns and SOLID are just tools created to overcome the shortcomings of bad OOP languages.
When I use Rust, I don't really think about design patterns or SOLID or anything like that. Sure, Rust has certain idiomatic patterns that are common in the ecosystem. But most of these patterns are very Rust-specific and come down to syntax rather than semantics. For instance the builder pattern, which is tbh also another tool to overcome one of Rust's shortcomings (inability to create big structs easily and flexibly).
I think you're completely correct that these things are dogma (or "circlejerking" if you prefer that term). Just be flexible and open minded in how you approach problems and try to go for the simplest solution that works. KISS and YAGNI are honestly much better principles to go by than SOLID or OOP design patterns.
The main thing you are missing is that "loose coupling" does not mean "create an interface". You can have all concrete classes and loose coupling or all classes with interfaces and strong coupling. Coupling is not about your choice of implementation, but about which part does what.
If an interface simplifies your code, then use interfaces, if it doesn't, don't. The dogma of "use an interface everywhere" comes from people who saw good developers use interfaces to reduce coupling, while not understanding the context in which it was used, and then just thought "hey so interfaces reduce coupling I guess? Let's mandate using it everywhere!", which results in using interfaces where they aren't needed, while not actually reducing coupling necessarily.
As a dev working on a large project using gradle, a lot of the time interfaces are useful as a means to avoid circular dependencies while breaking things up into modules. It can also really boost build times if modules don't have to depend on concrete impls, which can kill the parallelization of the build. But I don't create interfaces for literally everything, only if a type is likely going to be used across module boundaries. Which is a roundabout way of saying they reduce coupling, but just noting it as a practical example of the utility you gain.
I think a large part of interfaces everywhere comes from unit testing and class composition. I had to create an interface for a Time class because I needed to test for cases around midnight. It would be nice if testing frameworks allowed you to mock concrete classes (maybe you can? I haven't looked into it honestly) it could reduce the number of unnecessary interfaces.
You've been able to mock concrete classes in Java for like a decade or so, probably longer. As long as I can remember at least. Using Mockito it's super easy.
At least in C# with Moq you can only mock virtual methods of concrete classes, so using interfaces is still nicer in general.
Yeah Moq is what I used when I worked with .NET.
On an unrelated note; god I miss .NET so much. Fuck Microsoft and all that, but man C# and .NET feels so good for enterprise stuff compared to everything else I've worked with.
This was definitely true in the Java world when mocking frameworks only allowed you to mock interfaces.
Also the more I get into languages like Rust, the more these doubts are increasing and leading me to believe that most of it is just dogma that has gone far beyond its initial motivations and goals and is now just a mindless OOP circlejerk.
There are definitely occasions when these principles do make sense, especially in an OOP environment, and they can also make some design patterns really satisfying and easy.
Congratulations. This is where you wind up, long after learning the basics and start interacting with lots of code in the wild. You are not alone.
Implementing things with pragmatism, when it comes to conventions and design patterns, is how it's really done.
I think that OOP is most useful in two domains: Device drivers and graphical user interfaces. The Linux kernel is object-oriented.
OOP might also be useful in data structures. But you can as well think about them as "data structures with operations that keep invariants" (which is an older concept than OOP).
Yep, streams, pipes and files are all good examples of things that are an entity with associated operations.
OOP is good in a vacuum. In real life, where deadlines apply, you're going to get some ugly stuff under the hood, even though the app or system seems to work.
I think the general path to enlightenment looks like this (in order of experience):
- Learn about patterns and try to apply all of them all the time
- Don't use any patterns ever, and just go with a "lightweight architecture"
- Realize that both extremes are wrong, and focus on finding appropriate middle ground in each situation using your past experiences (aka, be an engineer rather than a code monkey)
Eventually, you'll end up "rediscovering" some parts of SOLID on your own, applying them appropriately, and not even realize it.
Generally, the larger the code base and/or team (which are usually correlated), the more that strict patterns and "best practices" can have a positive impact. Sometimes you need them because those patterns help wrangle complexity, other times it's because they help limit the amount of damage incompetent teammates can do.
But regardless, I want to point something out:
the more these doubts are increasing and leading me to believe that most of it is just dogma that has gone far beyond its initial motivations and goals and is now just a mindless OOP circlejerk.
This attitude is a problem. It's an attitude of ignorance, and it's an easy hole to fall into, but difficult to get out of. Nobody is "circlejerking OOP". You're making up a strawman to disregard something you failed at (eg successful application of SOLID principles). Instead, perform some introspection and try to analyze why you didn't like it without emotional language. Imagine you're writing a postmortem for an audience of colleagues.
I'm not saying to use SOLID principles, but drop that attitude. You don't want to end up like those annoying guys who discovered their first native programming language, followed a Vulkan tutorial, and now act like they're on the forefront of human endeavor because they imported a GLTF model into their "game engine" using assimp...
A better attitude will make you a better engineer in the long run :)
I dunno, I've definitely rolled into "factory factory" codebases that are abstraction astronauts just going to town over classes that only have one real implementation over a decade and seen how far the cargo culting can go.
It's the old saying "give a developer a tool, they'll find a way to use it." Having a distataste for mindless dogmatic application of patterns is healthy for a dev in my mind.
You've described my journey to a tea. You eventually find your middle ground which is sadly not universal and thus, we shall ever fight the stack overflow wars.
One example is creating an interface for every goddamn class I make because of "loose coupling" when in reality none of these classes are ever going to have an alternative implementation.
That one is indeed objective horse shit. If your interface has only one implementation, it should not be an interface. That being said, a second implementation made for testing COUNTS as a second implementation, so context matters.
In general, I feel like OOP principals like are indeed used as dogma more often than not, in Java-land and .NET-land. There's a lot of legacy applications out there run by folks who've either forgotten how to apply these principles soundly, or were never taught to in the first place. But I think it's more of a general programming trend, than any problem with OOP or its ecosystems in particular. Betcha we see similar things with Rust, when it reaches the same age.
SOLID often comes up against YAGNI (you ain't gonna need it).
What makes software so great to develop (as opposed to hardware) is that you can (on the small scale) do design after implementation (i.e. refactoring). That lets you decide after seeing how your new bit fits in whether you need an abstraction or not.
If it makes the code easier to maintain it's good. If it doesn't make the code easier to maintain it is bad.
Making interfaces for everything, or making getters and setters for everything, just in case you change something in the future makes the code harder to maintain.
This might make sense for a library, but it doesn't make sense for application code that you can refactor at will. Even if you do have to change something and it means a refactor that touches a lot, it'll still be a lot less work than bloating the entire codebase with needless indirections every day.
True. Open-closed principal is particularly applicable to library code, but a waste much of the time in a consuming application, where you will be modifying code much more.
Getters and setters are superfluous in most cases, because you do not actually want to hide complexity from your users.
To use the usual trivial example : if you change your circle's circumference from a property to a function, I need to know ! You just replaced a memory access with some arithmetic ; depending in my behaviour as a user this could be either great or really bad for my performance.
Exactly this. And to know what code is easy to maintain you have to see how couple of projects evolve over time. Your perspective on this changes as you gain experience.
I call it mario driven development, because oh no! The princess is in a different castle.
You end up with seemingly no code doing any actual work.
You think you found the function that does the thing you want to debug? Nope, it defers to a different function, which calls a a method of an injected interface, which creates a different process calling into a virtual function, which loads a dll whose code lives in a different repo, which runs an async operation deferring the result to some unspecified later point.
And some of these layers silently catch exceptions eating the useful errors and replacing them with vague and useless ones.
I'm a firm believer in "Bruce Lee programming". Your approach needs to be flexible and adaptable. Sometimes SOLID is right, and sometimes it's not.
"Adapt what is useful, reject what is useless, and add what is specifically your own."
"Notice that the stiffest tree is most easily cracked, while the bamboo or willow survives by bending with the wind."
And some languages, like Rust, don't fully conform to a strict OO heritage like Java does.
"Be like water making its way through cracks. Do not be assertive, but adjust to the object, and you shall find a way around or through it. If nothing within you stays rigid, outward things will disclose themselves.
"Empty your mind, be formless. Shapeless, like water. If you put water into a cup, it becomes the cup. You put water into a bottle and it becomes the bottle. You put it in a teapot, it becomes the teapot. Now, water can flow or it can crash. Be water, my friend."
It's been interesting to watch how the industry treats OOP over time. In the 90s, JavaScript was heavily criticized for not being "real" OOP. There were endless flamewars about it. If you didn't have the sorts of explicit support that C++ provided, like a class keyword, you weren't OOP, and that was bad.
Now we get languages like Rust, which seems completely uninterested in providing explicit OOP support at all. You can piece together support on your own if you want, and that's all anyone cares about.
JavaScript eventually did get its class keyword, but now we have much better reasons to bitch about the language.
It's funny cause in C++, inheritance is almost frowned upon now cause of the performance and complexity hits.
It's been frowned upon for decades.
That leads us to our second principle of object-oriented design: Favor object composition over class inheritance
- Design Patterns - Elements of Reusable Object-Oriented Software (1994)
The funny thing is I really liked the old JS prototypal inheritance. :)
I have to wonder about how many practices in any field are really a 'best in all cases' rule vs just an 'if everyone does it like this we'll all work better together because we're all operating from the same rulebook, even if the rules are stupid,' thing or a 'this is how my pappy taught me to write it,' thing.
Yes OOP and all the patterns are more than often bullshit. Java is especially well known for that. "Enterprise Java" is a well known meme.
The patterns and principles aren't useless. It's just that in practice most of the time they're used as hammers even when there's no nail in sight.