Skip Navigation

It's probably time to stop recommending Clean Code @ Things Of Interest

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

76
76 comments
  • I felt the same when reading that book, and I never finished it because following the rules he suggested produced horrible code.

    If memory serves, he also suggested that the ideal if statement only had one line inside, and you should move multiple lines into a function to achieve this.

    I once had to work on a codebase that seemed like it had followed his style, and it was an awful experience. There were hundreds of tiny functions (most only used once) and even with an IDE it was a chore to follow the logic. Best case the compiler removed most of this "clean" code and the runtime wasn't spending most of its time managing the stack like a developer had to do.

    54
  • 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?

    38
    • 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.

      20
    • Super hard to understand what’s going on, since it’s too generic

      In other words, you wrote nice code, but forgot to document it?

      6
      • That's a good question, and I never through about it like that. I think that the lack of documentation isn't that much of a problem, rather that the code stands out in the project in that it is complex to understand and requires some more though, effort and imagination to grasp, since it's generic with lot of interfaces and polymorphism.

        Now, that usually wouldn't be much of an issue, however - the project is a game we've been actively working on in our spare time in a team of 2 programmers for the last 6 years, and we are all fed up with it and just want it to end. Most of the (pretty large by now) codebase is kind of simple - it's a game code, after all, and since we started it when we were 20, there aren't many overenginered ideas or systems, but everything is mostly written in the ugly, but simple and direct way, so if we had wanted to change something, we may have had to rewrite a part of it, but it never really needed much effort to understand what's going on.

        But now I need to change this code, which is one of the only parts that requires some kind of imagination and actually sitting down and trying to understand it, and since my motivation about the project is so low, it's a pretty large hurdle to cross. One that is also unnecessary, since most of the generalism isn't needed and will never be used. But since the code is written in such extensible way, it's hard to just hack up a simple and ugly solution somewhere into it and be done with it, without really figuring out what the hell is going on.

        A documentation wouldn't help with that - it would still take the same amount of mental effort to be able to work with that code, which we generally lack in the project. I think that if I actually took the time to properly look through the code, figuring out what's going on wouldn't be too hard - the naming convention is pretty ok and it's not that difficult, it just requires some mental effort.

        I'm not trying to make excuses, the code very probably has problems, I'm just trying to better sort my thoughts about why I have so much problems working on it. It probably has more to do with my motivation, rather than the code in itself, and the fact that the complexity here wasn't required, and is now a needless hurdle that actually hinders progress. Not due to it's quality, but do to unrelated motivation issues and us having to basically force ourselves to work on and finish the damn project.

        4
  • I feel like it’s wrong to idolize anything in the same way that it’s wrong to throw out many things (there are some clear exceptions usually in the realm of intolerance but that’s unrelated to this). Clean Code, like every other pattern in software development, has some good things and some bad things. As introduction to the uninitiated, it has many good things that can be built on later. But, like Gang of Four, it is not the only pattern we apply in our craft and, like Agile, blind devotion, turning a pattern into a prescription, to Clean Code is going to lead to a lot of shit code.

    Cognitive load helps us understand this problem a lot better. As a junior with no clue how to write production code, is Clean Code going to provide with a decent framework I can quickly learn to start learning my craft, should I throw it out completely because parts are bad, or should I read both Clean Code and all its criticism before I write a single line? The latter two options increase a junior’s extraneous cognitive load, further reducing the already slim amount of power they can devote to germane cognitive load because their levels of intrinsic are very high by the definition of being a junior.

    Put a little bit differently, perfection (alternatively scalable, maintainable, shipped code) comes from learning a lot of flawed things and adapting those patterns to meet the needs. I am going to give my juniors flawed resources to learn from to then pick and choose when I improve those flaws. A junior has to understand the limitations of Clean Code and its failures to really understand why the author is correct here. That’s more cognitive science; we learn best when we are forming new connections with information we already know (eg failing regularly). We learn worse when someone just shows us something and we follow it blindly (having someone solve your problem instead of failing the problem a few times before getting help).

    I’m gonna be super hand-wavy with citations here because this a soapbox for me. The Programmer’s Brain by Felienne Hermans does a good job of pulling together lots of relevant work (part 2 IIRC). I was first introduced to cognitive load with Team Topologies and have since gone off reading of bunch of different things in pedagogy and learning theory.

    23
  • IMO it would be best to present code that fits in a single file to people on the net and refactor it to their liking while keeping the same functionality, then let others rate the style and pick a few characteristics. Then the code can be ranked by the different characteristics to obtain the top 5 or so "cleanest" coding styles.

    I feel it's worth doing an entire study instead of relying on what one dude says is clean code. Let the people speak.

    Anti Commercial-AI license

    10
  • No, it isn't

    0
  • 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...

    -4
You've viewed 76 comments.