As a Java engineer in the web development industry for several years now, having heard multiple times that X is good because of SOLID principles or Y is bad because it breaks SOLID principles, and having to memorize the “good” ways to do everything before an interview etc, I find it harder and harder to do when I really start to dive into the real reason I’m doing something in a particular way.

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.

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.

What are your opinions on this?

  • FizzyOrange@programming.dev
    link
    fedilink
    arrow-up
    16
    arrow-down
    1
    ·
    6 days ago

    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.

      • FizzyOrange@programming.dev
        link
        fedilink
        arrow-up
        3
        ·
        5 days ago

        Even KISS. Sometimes things just have to be complex. Of course you should aim for simplicity where possible, but I’ve seen people fight against better and more capable options just because they weren’t as simple and thus violated the KISS “rule”.

  • douglasg14b@lemmy.world
    link
    fedilink
    arrow-up
    9
    ·
    6 days ago

    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.

    • XM34@feddit.org
      link
      fedilink
      English
      arrow-up
      1
      ·
      edit-2
      5 days ago

      And then you have clean code. Clean code is like cooking with California Reapers. Some people swear on it and a tiny bit of Clean Code in your code base has never hurt anyone. But use it as much as the book recommends and I’m gonna vomit all day long.

  • JakenVeina@midwest.social
    link
    fedilink
    arrow-up
    27
    ·
    edit-2
    7 days ago

    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.

    • egerlach@lemmy.ca
      link
      fedilink
      English
      arrow-up
      7
      ·
      7 days ago

      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.

    • boonhet@sopuli.xyz
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      7 days ago

      Yeah… Interfaces are great, but not everything needs an interface.

      I ask myself: How likely is this going to have an alternative implementation in the future?

      If the answer is “kinda likely”, it gets an interface. If the answer is “idk, probably not? Why would it?” then it does not get an interface.

      Of course these days it’s more likely to be an unnecessary trait than an unnecessary interface. For me, I mean.

  • entwine@programming.dev
    link
    fedilink
    arrow-up
    21
    arrow-down
    2
    ·
    7 days ago

    I think the general path to enlightenment looks like this (in order of experience):

    1. Learn about patterns and try to apply all of them all the time
    2. Don’t use any patterns ever, and just go with a “lightweight architecture”
    3. 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 :)

    • iByteABit@programming.devOP
      link
      fedilink
      arrow-up
      1
      ·
      3 days ago

      I get your points and agree, though my “attitude” is mostly a response to a similar amount of attitude deployed by the likes of developers who swear by one principle to the death and when you doubt an extreme usage of these principles they come at you by throwing acronyms instead of providing any logical arguments as to why you should always create an interface for everything

    • marzhall@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      6 days ago

      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.

    • Gonzako@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      6 days ago

      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.

  • Azzu@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    15
    arrow-down
    1
    ·
    6 days ago

    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.

    • HereIAm@lemmy.world
      link
      fedilink
      arrow-up
      6
      ·
      6 days ago

      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.

      • Guttural@jlai.lu
        link
        fedilink
        Français
        arrow-up
        2
        ·
        3 days ago

        I’ve had to do that too, for tests specifically as well, and making clocks an interface on the spot was trivial. I did it when I needed it though, and not ahead of time.

        A Time interface is waaaay too broad. Turns out, I only needed something something to give me programmable ticks for my tests, which is much narrower in scope than abstracting something as general as time.

        I’d say abstractions designed to support tests need to be very narrow in scope, and focused on solving the problem at hand.

      • JackbyDev@programming.dev
        link
        fedilink
        English
        arrow-up
        4
        ·
        6 days ago

        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.

        • HereIAm@lemmy.world
          link
          fedilink
          arrow-up
          4
          ·
          6 days ago

          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.

      • sik0fewl@lemmy.ca
        link
        fedilink
        arrow-up
        1
        ·
        6 days ago

        This was definitely true in the Java world when mocking frameworks only allowed you to mock interfaces.

    • FunkFactory@lemmy.world
      link
      fedilink
      English
      arrow-up
      3
      ·
      6 days ago

      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.

  • JackbyDev@programming.dev
    link
    fedilink
    English
    arrow-up
    8
    ·
    6 days ago

    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.

  • deathmetal27@lemmy.world
    link
    fedilink
    arrow-up
    4
    ·
    5 days ago

    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.

    Not only loose coupling but also performance reasons. When you initialise a class as it’s interface, the size of the method references you load on the method area of the memory (which doesn’t get garbage collected BTW) is reduced.

    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.

    In my experience, not following SOLID principles makes your application an unmaintainable mess in roughly one year. Though SOLID needs to be coupled with better modularity to be effective.

  • Beej Jorgensen@lemmy.sdf.org
    link
    fedilink
    arrow-up
    18
    ·
    7 days ago

    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.”

    • Frezik@lemmy.blahaj.zone
      link
      fedilink
      English
      arrow-up
      11
      ·
      7 days ago

      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 removed about the language.

      • Brosplosion@lemmy.zip
        link
        fedilink
        arrow-up
        3
        ·
        7 days ago

        It’s funny cause in C++, inheritance is almost frowned upon now cause of the performance and complexity hits.

        • wicked@programming.dev
          link
          fedilink
          arrow-up
          3
          ·
          6 days ago

          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)
  • JackbyDev@programming.dev
    link
    fedilink
    English
    arrow-up
    5
    ·
    6 days ago

    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

  • Corbin@programming.dev
    link
    fedilink
    English
    arrow-up
    2
    ·
    5 days ago

    Java is bad but object-based message-passing environments are good. Classes are bad, prototypes are also bad, and mixins are unsound. That all said, you’ve not understood SOLID yet! S and O say that just because one class is Turing-complete (with general recursion, calling itself) does not mean that one class is the optimal design; they can be seen as opinions rather than hard rules. L is literally a theorem of any non-shitty type system; the fact that it fails in Java should be seen as a fault of Java. I is merely the idea that a class doesn’t have to implement every interface or be coercible to any type; that is, there can be non-printable non-callable non-serializable objects. Finally, D is merely a consequence of objects not being functions; when we want to apply a functionf to a value x but both are actually objects, both f.call(x) and x.getCalled(f) open a new stack frame with f and x local, and all of the details are encapsulation details.

    So, 40%, maybe? S really is not that unreasonable on its own; it reminds me of a classic movie moment from “Meet the Parents” about how a suitcase manufacturer may have produced more than one suitcase. We do intend to allocate more than one object in the course of operating the system! But also it perhaps goes too far in encouraging folks to break up objects that are fine as-is. O makes a lot of sense from the perspective that code is sometimes write-once immutable such that a new version of a package can add new classes to a system but cannot change existing classes. Outside of that perspective, it’s not at all helpful, because sometimes it really does make sense to refactor a codebase in order to more efficiently use some improved interface.

  • HaraldvonBlauzahn@feddit.org
    link
    fedilink
    arrow-up
    5
    ·
    6 days ago

    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).

      • Guttural@jlai.lu
        link
        fedilink
        Français
        arrow-up
        1
        ·
        edit-2
        3 days ago

        Those are very powerful abstractions for sure, but did you notice how far their implementation is from standard Java OOP?

        That’s because polymorphism at a macro-level is a functional concern, not something programmers speak in conferences about.

        One of my biggest gripe with Y2K-style OOP is that its proponents make lots of promises that don’t track in practice when you measure the outcomes. One such promise is that writing rigid class hierarchies lead to the potent abstractions you describe.

  • Windex007@lemmy.world
    link
    fedilink
    arrow-up
    10
    arrow-down
    1
    ·
    7 days ago

    Whoever is demanding every class be an implementation of an interface started thier career in C#, guaranteed.

      • Windex007@lemmy.world
        link
        fedilink
        arrow-up
        8
        ·
        7 days ago

        I’m my professional experience working with both, Java shops don’t blindly enforce this, but c# shops tend to.

        Striving for loosely coupled classes is objectively a good thing. Using dogmatic enforcement of interfaces even for single implementors is a sledgehammer to pound a finishing nail.

  • Sunsofold@lemmings.world
    link
    fedilink
    arrow-up
    7
    ·
    7 days ago

    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.

  • dejected_warp_core@lemmy.world
    link
    fedilink
    arrow-up
    5
    arrow-down
    1
    ·
    6 days ago

    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.