Seeing that Uncle Bob is making a new version of Clean Code I decided to try and find this article about the original.

  • dandi8@fedia.io
    link
    fedilink
    arrow-up
    7
    arrow-down
    2
    ·
    edit-2
    1 month ago

    It makes me sad to see people upvote this.

    Robert Martin’s “Clean Code” is an incredibly useful book which helps write code that Fits In Your Head, and, so far, is the closest to making your code look like instructions for an AI instead of random incantations directed at an elder being.

    The principle that the author of this article argues against seems to be the very principle which helps abstract away the logic which is not necessary to understand the method.

    public void calculateCommissions() {
      calculateDefaultCommissions();
      if(hasExtraCommissions()) {
        calculateExtraCommissions();
      } 
    } 
    

    Tells me all I need to know about what the method does - it calculates default commissions, and, if there are extra commissions, it calculates those, too. It doesn’t matter if there’s 30 private methods inside the class because I don’t read the whole class top to bottom.

    Instead, I may be interested in how exactly the extra commissions are calculated, in which case I will go one level down, to the calculateExtraCommissions() method.

    From a decade of experience I can say that applying clean code principles results in code which is easier to work with and more robust.

    Edit:

    To be clear, I am not condoning the use of global state that is present in some examples in the book, or even speaking of the objective quality of some of the examples. However, the author of the article is throwing a very valuable baby with the bathwater, as the actual advice given in the book is great.

    I suppose that is par for the course, though, as the aforementioned author seems to disagree with the usefulness of TDD, claiming it’s not always possible…

    • magic_lobster_party@kbin.run
      link
      fedilink
      arrow-up
      2
      ·
      1 month ago

      Robert Martin’s “Clean Code” is an incredibly useful book which helps write code that Fits In Your Head

      It has the exact opposite effect. It leads to code that doesn’t fit in your head.

      Because his style of coding leads to code where everything useful are 10 levels deep in nested method calls. If I want to understand how the code works and how my changes will affect the overall picture, I need to keep a dozen methods in my head and how all of these are connected to each other.

      And he’s mostly working with global states, so knowing where variables are assigned is just a huge pain.

      So many times I’ve looked into code like this where I finally find what I’m looking for, only to forget how I even reached this part of the code. So I need to backtrack to remember how I got there, but then I forget where that damn thing I was looking for is. And I go back and forth until I figure out a mental model of the code. It’s awful!

      Compare that with just having one big method. Everything is in front of me. I don’t need to keep that many things in my head at the same time, because everything I need is right there in front of my eyes.

      Sure, sometimes breaking out to separate methods can make it easier to communicate what the code does and the boundaries of each scope, but if it’s overdone it leads to code that’s impossible to work with.

      • dandi8@fedia.io
        link
        fedilink
        arrow-up
        1
        ·
        1 month ago

        Clean code does not prevent writing bad code, it just makes it a bit easier to write good code.

        OF COURSE you can follow the principles and still write bad code, because so much more goes into it, including skill.

        A giant method with everything laid out, potentially mixing abstractions sounds like a nightmare to me. It leads to cognitive overload.

        • magic_lobster_party@kbin.run
          link
          fedilink
          arrow-up
          3
          ·
          1 month ago

          I heavily disagree it’s easier to write good code if you follow clean code. Especially if you follow his examples throughout the book. Most of his examples are just over engineered messes held together by side effects (even if he says side effects is a bad thing).

          If he can’t write good code by following clean code, why should you? He even picked the examples himself and failed!

    • JackbyDev@programming.devOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 month ago

      I can’t judge this example without seeing what would be inside those other methods. As presented, what you say makes sense, but many of the full examples shown in the article show very strange combinations.

      I agree that private methods can help make code “fit in your head” but at the same time, dogmatically pursuing this can spread code out more which does the opposite.

    • Dave.@aussie.zone
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      1 month ago

      in which case I will go one level down, to the calculateExtraCommissions() method.

      In which case you will discover that the calculateExtraCommissions() function also has the same nested functions and you eventually find six subfunctions that each calculate some fraction of the extra commission, all of which could have been condensed into three lines of code in the parent function.

      Following the author’s idea of clean code to the letter results in a thick and incomprehensible function soup.

      • dandi8@fedia.io
        link
        fedilink
        arrow-up
        0
        ·
        1 month ago

        It’s only as incomprehensible as you make it.

        If there are 6 subfunctions, that means there’s 6 levels of abstraction (assuming the method extraction was not done blindly), which further suggests that maybe they should actually be part of a different class (or classes). Why would you be interested in 6 levels of abstraction at once?

        But we’re arguing hypotheticals here. Of course you can make the method implementations a complete mess, the book cannot guarantee that the person applying the principles used their brain, as well.

        • BatmanAoD@programming.dev
          link
          fedilink
          arrow-up
          1
          ·
          1 month ago

          Because abstractions leak. Heck, abstractions are practically lies most of the time.

          What’s the most time-consuming thing in programming? Writing new features? No, that’s easy. It’s figuring out where a bug is in existing code.

          How do abstractions help with that? Can you tell, from the symptoms, which “level of abstraction” contains the bug? Or do you need to read through all six (or however many) “levels”, across multiple modules and functions, to find the error? Far more commonly, it’s the latter.

          And, arguably worse, program misbehavior is often due to unexpected interactions between components that appear to work in isolation. This means that there isn’t a single “level of abstraction” at which the bug manifests, and also that no amount of unit testing would have prevented the bug.

          • dandi8@fedia.io
            link
            fedilink
            arrow-up
            0
            ·
            1 month ago

            How do abstractions help with that? Can you tell, from the symptoms, which “level of abstraction” contains the bug? Or do you need to read through all six (or however many) “levels”, across multiple modules and functions, to find the error?

            I usually start from the lowest abstraction, where the stack trace points me and don’t need to look at the rest, because my code is written well.

    • realharo@lemm.ee
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      1 month ago

      Why is it a void method? This only tells me that some state is mutated somewhere, but the effect is neither visible nor documented.

      I would expect a function called “calculate” to just return a number and not have any side effects.

      • dandi8@fedia.io
        link
        fedilink
        arrow-up
        0
        ·
        1 month ago

        You’re nitpicking.

        As it happens, it’s just an example to illustrate specifically the “extract to method” issues the author had.

        Of course, in a real world scenario we want to limit mutating state, so it’s likely this method would return a Commission list, which would then be used by a Use Case class which persists it.

        I’m fairly sure the advice about limiting mutating state is also in the book, though.

        At the same time, you’re likely going to have a void somewhere, because some use cases are only about mutatimg something (e.g. changing something in the database).

  • Mikina@programming.dev
    link
    fedilink
    arrow-up
    2
    ·
    1 month ago

    There’s a piece of code in our hobby game project that I’ve written after attending classes in college about how to write clean and SOLID code. It’s the most overengineered piece of shit I’ve ever written. I’m not saying it’s the fault of the lectures, of course it’s on me being a little bit over zealous, but it does check all the boxes - It’s a simple “show selectable list of stuff”, follows MVC, it’s extensible without rewriting to adittional data-types and formats, extensible view that can show any part of data you need, generic, and in general it could be used anywhere we need, for any kind of data.

    There’s only one place where we need and use such list in our game.

    I needed to rewrite a part of it, since the UI changed drastically, to not need this kind of list, while also adding events into the process. I haven’t seen the code for almost 4 years, and it’s attrocious. Super hard to understand what’s going on, since it’s too generic, interfaces and classes all over the place, and while it probably would be possible to rewrite the views for the new features we need, it’s just so complex that I don’t have the mental capacity to again figure out how it was supposed to work and properly wire it up again.

    I’m not saying it’s fault of the classes, or SOLID. It’s entirely my fault, because the classes inspired and hyped me with ideas about what a clean code should look like, that I didn’t stop and think whether it’s really needed here, and went over-the-top and overengineered the solution. That’s what I’d say is the danger of such Clean Code books and classes - it’s easy to feel clever for making something that passes SOLID to the letter, but extensibility usually comes at a complexity, and it’s super important to stop and think - do I really need it?

    • magic_lobster_party@kbin.run
      link
      fedilink
      arrow-up
      3
      ·
      1 month ago

      Academia has a disconnect with the industry when it comes to coding practices. Most teachers haven’t much industry experience. They don’t know how it is to maintain a 10 year old project using SOLID and clean code principles.

      They just teach whatever has already been in the curriculum for decades. They don’t know that whatever they’re teaching is actually harmful in the industry.