News for hackers. Stuff that matters.
Re:There’s No Such Thing as Clean Code by onion2k on Friday January 28, 2022 @03:32AM (#30112122)
The Uncle Bob Martin definition of "clean code" from his book "Clean Code: A Handbook of Agile Software Craftsmanship" is a set of rules that absolutely are not at odds with one another. If you follow them you will end up with code that's really nice to read and easier to maintain, and, most importantly, that you can confidently change.

There's a decent summary here - https://gist.github.com/wojteklu/73c6914cc446146b8b533c0988c...

Re:There’s No Such Thing as Clean Code by yakubin on Friday January 28, 2022 @04:14AM (#30112403)
> If you follow them you will end up with code that's really nice to read and easier to maintain, and, most importantly, that you can confidently change.

I found that a lot of those guidelines lead to the exact opposite. Examples:

- Prefer polymorphism to if/else or switch/case (oh, the joy of tracing a simple task through 50 files)

- Use dependency injection (same as above)

- Hide internal structure (that "private" is the default in C++, Java and Rust just adds to boilerplate; the default case is that you want everything public (unless you like writing trivial getters and setters just for the fun of it); legitimate uses of "private" exist, but are rare)

- One assert per test (I guess I need to throw my property-based tests out the window)

Re:There’s No Such Thing as Clean Code by karatinversion on Friday January 28, 2022 @04:40AM (#30112581)
> Hide internal structure (that "private" is the default in C++, Java and Rust just adds to boilerplate; the default case is that you want everything public (unless you like writing trivial getters and setters just for the fun of it); legitimate uses of "private" exist, but are rare)

This is such a strange POV to me. There are cases where your classes are just data records, but any object with logic surely wants to restrict access to its state so it can maintain invariants? E.g. the simplest classes I can think of, like a vector that consists of a pointer to a buffer, a length and a capacity, wouldn't want to provide a setter for the buffer, or allow callers to set it to a random value?

Re:There’s No Such Thing as Clean Code by lifeisstillgood on Friday January 28, 2022 @05:39AM (#30112970)
This is where Python's "consenting adults" idea comes in. Why the hell should someone change a vector "mid-flight" - that is a huge code smell and should be dealt with in the review / design / discussion / pub.

But there are reasons and rationales to "lockdown" the code (beyond not trusting fellow devs!) and at that point I suggest that any mutable state language cannot be properly locked down - so use a functional language where its easier to reason about.

Usually (IMO) private classes etc are just trying to make it easier to reason about state changes - when its probably best to dump the whole mess.

Re:There’s No Such Thing as Clean Code by hogrider on Saturday January 29, 2022 @06:43AM (#30125584)
In most situations, no you can not trust fellow devs to do the right thing and if you push for those reviews then you're the asshole and lose the office politics game. So yeah, I'd rather guardrail people into good behavior.
Re:There’s No Such Thing as Clean Code by lifeisstillgood on Sunday January 30, 2022 @05:14AM (#30135566)
Can I clarify? You get a review that just looks wrong and you become the asshole? What kind of reviews are these ?
Re:There’s No Such Thing as Clean Code by lifeisstillgood on Sunday January 30, 2022 @12:30PM (#30139094)
please ignore the above. I am sure reviews can become political arenas
Re:There’s No Such Thing as Clean Code by yakubin on Friday January 28, 2022 @05:06AM (#30112738)
IME I write about 10-20 as many data records as "classes". I do use "private" from time to time to protect invariants. But if the default was switched to public, my code on balance would be a lot shorter and readable. I'm not saying that "private" shouldn't exist. I'm saying it's a poor default.
Re:There’s No Such Thing as Clean Code by tubs on Friday January 28, 2022 @05:16AM (#30112808)
In c++ you choose the default so I'm not sure it should be lumped in with the others.
Re:There’s No Such Thing as Clean Code by rightbyte on Friday January 28, 2022 @05:31AM (#30112907)
In the literal sense private is default though. I wonder why. Lexical sorting of private, protected and public?

It makes almost no sense to have all private class fields and methods. The only thing I can think of is fat handles with friend classes or functions.

Re:There’s No Such Thing as Clean Code by andi999 on Friday January 28, 2022 @05:56AM (#30113084)
In c++ a class declared with the keyword 'class' is private by default, and a class declared with the keyword 'struct' is public by default. So if you want public by default just write 'struct' (somehow this is not commonly known).

https://en.wikipedia.org/wiki/C%2B%2B_classes

Re:There’s No Such Thing as Clean Code by klodolph on Friday January 28, 2022 @06:35AM (#30113391)
I think the “somehow” is not too hard to guess… it is easy to think that “class” is the only way to declare a class, especially since “struct” is taken from C. I’m sure very few intro C++ explanations ever mention “struct” as a way to declare a class.
Re:There’s No Such Thing as Clean Code by TillE on Friday January 28, 2022 @04:12PM (#30120424)
There's quite a lot about C++ which wouldn't be covered in a simple tutorial, but the meaning of class/struct is a pretty basic part of the language, hardly obscure.

Maybe it's something you could miss if you're coming from a strong C background and still thinking of C++ as C-with-classes, but otherwise it's something any serious user of the language would pick up.

Re:There’s No Such Thing as Clean Code by klodolph on Saturday January 29, 2022 @01:06AM (#30124161)
This comment represents some of the worst elitism I see in the programming community.
Re:There’s No Such Thing as Clean Code by andi999 on Friday January 28, 2022 @07:35AM (#30113801)
Yes, I meant common introductory material should mention (and use) it (I think Bjarne does mention it, but I wouldnt call it introductory material)
Re:There’s No Such Thing as Clean Code by tubs on Friday January 28, 2022 @05:57AM (#30113094)
struct over class then.
Re:There’s No Such Thing as Clean Code by tjajab on Friday January 28, 2022 @04:53AM (#30112648)
I guess that for a lot of people it is the case where you have a class with mutable state that is the anomaly.
Re:There’s No Such Thing as Clean Code by barrkel on Friday January 28, 2022 @06:56AM (#30113550)
It's nuts. It's deeply naive and utterly unworkable.
Re:There’s No Such Thing as Clean Code by rootlocus on Friday January 28, 2022 @04:28AM (#30112496)
> One assert per test

Was meant to make a failed test instantly communicate what's wrong with the unit under test. As frameworks evolve and our practices around them change, it's absolutely fine to come up with new rules.

All of uncle Bobs rules come with pages of explanations of what problems they solve. If you don't have those problems, you may not need those solutions. The book is more about the spirit of the law than the letter.

Re:There’s No Such Thing as Clean Code by pydry on Friday January 28, 2022 @05:54AM (#30113072)
This makes no sense whatsoever.

I've read thousands of tests with multiple asserts that were perfectly plain about what was being tested and in many cases the logical sequence made it easier to understand.

Breaking them into separate tests with one assert would simply have meant a shit ton more code to read.

Uncle Bob's "rules" often come with mile long caveats that he seems to be blissfully unaware of. I think I get why he said it - overloaded tests are a thing - but he fucked up trying to turn his observation into a rule.

Re:There’s No Such Thing as Clean Code by MaxBarraclough on Saturday January 29, 2022 @06:48PM (#30131871)
> > One assert per test

> Was meant to make a failed test instantly communicate what's wrong with the unit under test. As frameworks evolve and our practices around them change, it's absolutely fine to come up with new rules.

To put a finer point on that then, you're saying the rule is obsolete if your test framework allows assert statements to accept message strings?

Re:There’s No Such Thing as Clean Code by MauranKilom on Friday January 28, 2022 @04:38AM (#30112569)
> the default case is that you want everything public (unless you like writing trivial getters and setters just for the fun of it); legitimate uses of "private" exist, but are rare

Any time a class has invariants to maintain, you probably need private. That could be infrequent in your experience, but overall, invariants are a cornerstone of abstractions.

Re:There’s No Such Thing as Clean Code by rightbyte on Friday January 28, 2022 @05:02AM (#30112699)
I agree with my whole heart.

I think you forgot one of the most devastating points in Clean Code, namely short functions and DRY (when overdoing it). It leads to the same problem "jumping through 50 files".

My favorite subversive action to write clean code is using jumps. There is no end to how convoluted code people write to avoid the oh so harmful leap.

I feel like much of the need for short functions comes from nesting ifs moving to code more and more to the right. That is easily solved by jumps or early returns. Early returns can lead to too many functions if not doing with modesty, though.

EDIT: Dogmas taking the developer out of his comfort zone is probably the main culprit. Any advice is good, as long as you don't follow it.

Re:There’s No Such Thing as Clean Code by ziml77 on Friday January 28, 2022 @09:23AM (#30114910)
These are the points that I've come to despise from Clean Code. After I read that book I wrote code that tried to follow all those rules. It didn't seem bad at the time, but returning to code written that way has always sucked hard. You have to jump around far too much with the 7 line function rule. And over-DRY code can actually be harder to change since repetition is often incidental. In that case a change in one place will actually affect more than its supposed to.
Re:There’s No Such Thing as Clean Code by BrS96bVxXBLzf5B on Friday January 28, 2022 @08:15AM (#30114163)
Could you provide an example of how you would restructure some nested ifs to jumps? I can kind of picture it but can also imagine doing it badly and being in pain.
Re:There’s No Such Thing as Clean Code by Toine on Friday January 28, 2022 @04:44AM (#30112605)
These are minor inconveniences relative to the massive benefits all these techniques bring to your codebase. "Go to implementation" shortcut is a thing and solves the first 2.

"One assert per test" -> just test one "concept" per test, not literarily 1 assert statement per test. IE :

  assert(loc.x == 10.3)
  assert(loc.y == 10.4)
  assert(loc.z == 10.5)
is absolutely fine (even you could do it in 1 assert).
Re:There’s No Such Thing as Clean Code by pydry on Friday January 28, 2022 @06:21AM (#30113263)
If he meant one "concept" he probably would have said that.

In any case, "concept" being such a loosely defined thing would render the rule a bit pointless.

There is something deeper behind this rule but think he fucked up articulating what it was.

Re:There’s No Such Thing as Clean Code by jseban on Friday January 28, 2022 @04:50AM (#30112626)
I agree, these rules make the code extremely verbose and over abstracted and scattered. The resulting code base will be several times the size of a straight forward solution, and this has a very big negative impact on maintainability.

Even if you use many files and only put a tiny amount of logic in each of them, you still need to have an understanding of the overall system and how things work together, and scattering things in more places, each with multiple wrappings, just makes that even harder.

Re:There’s No Such Thing as Clean Code by kgeist on Friday January 28, 2022 @01:03PM (#30118190)
>the default case is that you want everything public

I'd say that's only true for objects whose purpose is to merely group semi-related data together for copy/transfer/storage, nothing more. If your object has invariants/rules, it's a completely different story.

>legitimate uses of "private" exist, but are rare

So if you have a domain object with certain invariants (and any serious project has plenty of those), who makes sure its invariants are always upheld? If everyone is allowed to put whatever they want inside your object, it's easy to end up with inconsistent/corrupt data. Ideally, you want all the logic which deals with object mutations and their validation to be concentrated in one single place, and force all clients to use it, to avoid copypasting same code every time you mutate an object (which is error-prone). That single place can be the object itself, in the constructor or the setters, with fields made private (or const) to defend against misuse. Of course, you can validate object state in a separate function and treat your objects as just dumb bags of data, but then we aren't talking about OOP anymore (where the argument "private vs. public" actually makes sense).

Re:There’s No Such Thing as Clean Code by yakubin on Friday January 28, 2022 @01:58PM (#30118866)
I've partially responded to that already[1][2]. Let me add to that, that when you group together data, a lot of invariants are preserved by the logic you already wrote for the types you compose your types of. Most basic grouping of objects is a struct. It's a product type. Often what I want really is the product of all the states of the component types, without eliminating any combinations. And in the case of sum types: I practically always want all the states. All the invariants are already handled in the types being summed. Not for transfer/storage, but because it logically makes sense. Another thing is that "make invalid state unrepresentable" is a good guideline, but it can lead you into a corner, where you write a lot of code just to make sure that your type system proves that a certain error is not representable by a type, when this error is trivial, unlikely and would take seconds to debug at runtime (maybe would even be so obvious, as to not need any debugging at all). Just because a certain invariant is true in a domain, doesn't mean that it needs to be encoded in the type system. We need to choose what's the most productive use of our time as developers. Those are trade-offs.

I do not deny the usefulness of ADTs (which is what "private" is all about). I question whether it's the right default. My experience tells me it isn't. Whether you call my style of programming "OOP", is irrelevant to me. This term, after decades of evolution, doesn't mean anything anymore. I certainly don't treat it as a badge of quality and don't consult any definition of it before judging whether a particular technique in programming is useful or not.

[1]: <https://news.ycombinator.com/item?id=30112738>

[2]: <https://news.ycombinator.com/item?id=30113932>

Re:There’s No Such Thing as Clean Code by barrkel on Friday January 28, 2022 @06:56AM (#30113546)
Private - or visibility scoping generally - is absolutely essential when you program in an environment with multiple teams.

In its absence other people take dependencies on internal details which makes code much harder to refactor later. Lack of scoping on visibility increases the surface area of your APIs and increases the likelihood of bugs. Lack of scoping of visibility on state means losing almost all control over preservation of invariants.

Re:There’s No Such Thing as Clean Code by yakubin on Friday January 28, 2022 @07:51AM (#30113932)
I agree. What I'm saying is that private is a poor default.
Re:There’s No Such Thing as Clean Code by thfuran on Friday January 28, 2022 @08:33AM (#30114333)
Why? I think it's pretty reasonable to only expand APIs deliberately.
Re:There’s No Such Thing as Clean Code by yakubin on Friday January 28, 2022 @08:58AM (#30114609)
Because most things in healthy programs aren't APIs, but just plain data: records, sum types and composites thereof (arrays, stacks, queues, whatever).

Fred Brooks:

> Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won’t usually need your flowcharts; they’ll be obvious.

In that situation, having to sprinkle "public"/"pub" all over them is very noisy and unproductive. It's the worst with Rust's enums, which require you to declare the visibility of variants and enclosed types of variants separately, so you see a couple "pub"-s per line of an enum definition.

That may be an argument for separate keywords for plain data, and for "APIs", although I'm not sure how I feel about it. Underneath it's just coupling of data with functions. Some languages have separate keywords for those, some don't.

Re:There’s No Such Thing as Clean Code by ravenstine on Friday January 28, 2022 @10:59AM (#30116302)
How else is "Uncle" Bob Martin supposed to sell more books and presentations if people actually write code they can understand and are happy with? :D
Re:There’s No Such Thing as Clean Code by YEwSdObPQT on Friday January 28, 2022 @04:45AM (#30112612)
In simple cases many of the suggestions in the clean code book make things more complicated.

However when you get to larger programs big if ... else blocks become painful. Not having a DI mechanism becomes painful. Encapsulation becomes important because you will find other parts of the program modifying something when it really shouldn't be.

I used a moan about these things until they were taken away and then I really missed them.

Re:There’s No Such Thing as Clean Code by rhdunn on Friday January 28, 2022 @04:41AM (#30112593)
> One assert per test

I read that as "one operation per test". Specifically, both:

    assertEqual(complex(1, 2) + complex(3, 5),
                complex(4, 7));
and:

    auto c = complex(1, 2) + complex(3, 5);
    assertEqual(c.re(), 4);
    assertEqual(c.im(), 7);
would both satisfy that requirement, but:

    assertEqual(complex(1, 2) + complex(3, 5),
                complex(4, 7));
    assertEqual(complex(1, 2) + complex(-3, -5),
                complex(-2, -2));
would not. -- In that case, Clean Code is advocating having something like a "test adding positive complex numbers" and "test adding a positive complex number to a negative one". This makes it clear in the test failure report which of the operations and conditions has failed.
Re:There’s No Such Thing as Clean Code by SideburnsOfDoom on Friday January 28, 2022 @04:56AM (#30112663)

    assertEqual(c.re(), 4);
    assertEqual(c.im(), 7);
This is "logically" one assert, on two lines. So I would welcome it

In cases like this I find myself extracting a helper assert method if it gets used 3 or more times.

e.g.

  assertEqualComplex(c, 4, 7); 
which contains

    assertEqual(c.re(), x);
    assertEqual(c.im(), y);
Which makes that a bit clearer. This is not the best example but you can get the idea. I might have 5-10 lines in an assertTransactionSuceeded(txn) method.
Re:There’s No Such Thing as Clean Code by ziml77 on Friday January 28, 2022 @09:28AM (#30114987)
Your interpretation is a saner way to write tests, but I saw no indication that's what he meant. If it was what he meant, then he should have made it clearer. It's not like he couldn't have added an extra couple pages with an explanation and examples.
Re:There’s No Such Thing as Clean Code by pydry on Friday January 28, 2022 @06:25AM (#30113318)
>I read that as "one operation per test".

This replaces a clear, well defined thing (assert) with a vague, ill defined thing. What constitutes an "operation"?

There's almost no point to the rule if it isnt trivial to distinguish one operation from a group.

Re:There’s No Such Thing as Clean Code by foofye on Friday January 28, 2022 @05:10AM (#30112766)
And then you get a release mode warning about maybe unused c, unless you stick c in your code.
Re:There’s No Such Thing as Clean Code by BigJ1211 on Friday January 28, 2022 @05:16AM (#30112811)
Also if you look at the code samples he provides they often don't follow his own rules. (At least that was the case when I read the book, don't know if it's been revised since)
Re:There’s No Such Thing as Clean Code by nfw2 on Friday January 28, 2022 @11:14AM (#30116555)
IMO it's generally good advice to be wary of rolling too many disjoint use cases into one abstraction, and I think that's the rationale behind recommending polymorphism, but the ergonomics of actually writing OO code make it impractical advice to try to follow as a rule. Inversion-of-control is often a more practical way to solve the same problem, in my experience
Re:There’s No Such Thing as Clean Code by addicted on Friday January 28, 2022 @05:53AM (#30113067)
If you’re just using your classes as data records, most languages have better options for you.

Java has the record type. C# has struct and also property members that serve the same purpose.

I bet C++ and Rust also have better options for data records than plain classes.

Re:There’s No Such Thing as Clean Code by yakubin on Friday January 28, 2022 @07:49AM (#30113914)
Unfortunately, Rust doesn't. Rust only has "struct"-s, no classes, and they're private by default. Enums require the "pub" keyword once before a variant name, and once inside tuple associated with the variant. So often it's two "pub" keywords per line in an enum definition. So after writing code for a while, I then, upon compilation, need to fix up tens of "pub" occurrences. Pretty annoying. Wasted time, and the end result is hideous.
Re:There’s No Such Thing as Clean Code by cerved on Friday January 28, 2022 @06:47AM (#30113475)
> Prefer polymorphism to if/else or switch/case (oh, the joy of tracing a simple task through 50 files)

If this is arduous it may be worthwhile to change your dev environment

Re:There’s No Such Thing as Clean Code by ziml77 on Friday January 28, 2022 @09:38AM (#30115105)
I can hit F12 or Ctrl-Click to navigate to definitions and I get a back button to return to where I was across those navigation operations. Yet it still sucks to bounce through all those files. And it's usually not just a matter of drilling down through the calls, but also moving back up to find out how a dependency of a class that I've ended up in is initialized.
Re:There’s No Such Thing as Clean Code by moonchrome on Friday January 28, 2022 @04:07AM (#30112333)
>If you follow them you will end up with code that's really nice to read and easier to maintain, and, most importantly, that you can confidently change.

I remember going over unclebob github a while back and this wasn't the impression I got. The parts I read seemed like obtuse code that added pointless abstraction for the sake of following some convention.

Re:There’s No Such Thing as Clean Code by Hermitian909 on Friday January 28, 2022 @04:28AM (#30112492)
This is my experience as well. It takes a long time to grok what the program does and how it does it because of all the layers of indirection, even when the program is fairly simple. While this style might make small changes easy if you understand the code, the inertia to larger changes because of the layers of indirection is immense.
Re:There’s No Such Thing as Clean Code by Rygu on Friday January 28, 2022 @08:23AM (#30114232)
Wow that's on-point, agree that in many past projects "inertia to larger changes" is exactly how I would describe so-called Clean Code.

It's that small changes like adding getters and setters give an easy-to-follow PR, a false sense of accomplishment for both the author and the reviewer, and ton of dopamine with that. While important changes, that actually deliver business value or repay technical debt, become increasingly difficult to do without replacing massive amounts of code and redoing the file structure.

Inertia is the biggest demotivator.

Re:There’s No Such Thing as Clean Code by jseban on Friday January 28, 2022 @08:42AM (#30114437)
Yeah, you can't eliminate essential complexity by narrowing your view, it will just obscure the big picture.
Re:There’s No Such Thing as Clean Code by bryanrasmussen on Friday January 28, 2022 @04:23AM (#30112461)
>If you follow them you will end up with code that's really nice to read and easier to maintain, and, most importantly, that you can confidently change.

In Java (or Java like languages [C#]), which is the language they were really written for. There are many languages for which the precepts of Clean Code are not that great a fit.

Also - It's probably time to stop recommending Clean Code - https://qntm.org/clean

Re:There’s No Such Thing as Clean Code by jwdunne on Friday January 28, 2022 @05:07AM (#30112743)
Was just about to comment with this link. A useful counterpoint whenever Clean Code comes up.

I do think this article makes a lot of valid criticisms and it’s well worth a read. I remember being influenced by it early on but then re-read it as I gained more experience. This article sums up what I felt on rereading.

Re:There’s No Such Thing as Clean Code by andrekandre on Friday January 28, 2022 @07:32PM (#30122428)
maybe its time for someone to write "clean code: the good parts"
Re:There’s No Such Thing as Clean Code by dt3ft on Friday January 28, 2022 @04:09AM (#30112358)
> Don't comment out code, just remove

I hate commented out code, but this doesn't work in every situation. While working on a clinical information system, we had frequent "please bring it back" requests that came a few months after things were removed (also by request). Relying on source control is nice, but unless you document every feature/removal or use very extensive commit messages, finding the code that was removed (especially if it was spread over several classes) is easier if you just comment it out and write why it was commented out.

The important part here is to add why something was commented out.

Re:There’s No Such Thing as Clean Code by dtech on Friday January 28, 2022 @04:32AM (#30112523)
Seems garbage advice to me. Now you've polluted your production code, likely forever. I don't see a situation where using an issue tracker + good commits - e.g. 1 commit for the 1 PR linked to the ticket that removes the feature - won't result in a superior situation from a maintenance and traceability perspective.
Re:There’s No Such Thing as Clean Code by serial_dev on Friday January 28, 2022 @05:07AM (#30112747)
Yes, I agree with you. Another thing is how do you know which of the removed features will be asked to be put back? You can't really know.

You can guess, but that will leave you with many classes/functions commented out only in the hope that one day one of them will be requested to be put back. This leads to nobody daring to remove old code for months/years.

Then, one day, a new coder on the team just ask WTF is this here, realize it's outdated and not running anymore, and will remove the whole thing. Then, the "senior" guy on the team comes shouting at him/her: "We might need it one day, what do you know about our team, bla bla bla, we had a feature 4 years ago that was removed and we had to add back, so now we have the rule of commenting things out". Yuck, what a nightmare.

Just create good commit messages, don't group irrelevant changes together, and you'll be able to find the commit if you really have to. And even if you can't, you know you can still just develop that thing again, right?

Re:There’s No Such Thing as Clean Code by RMPR on Thursday February 03, 2022 @11:14AM (#30194272)
One solution might be to create a separate branch with the feature still available. With time, you kind of get the gut feeling of what could be useful later and what could not.
Re:There’s No Such Thing as Clean Code by zozbot234 on Friday January 28, 2022 @06:53AM (#30113517)
I thought the best practice for "keeping stuff around just in case" was an attic/ directory in the source repo. That way you're still benefiting from version control, but it's also clear that the code is totally untested and might bitrot at any time.
Re:There’s No Such Thing as Clean Code by dtech on Friday January 28, 2022 @08:19AM (#30114194)
There's little advantage over that vs looking up the old files in source control.
Re:There’s No Such Thing as Clean Code by dt3ft on Friday January 28, 2022 @05:18AM (#30112824)
Sure, in a normal world. But with 2 devs in team, handling a clinical information system in very rapidly changing environment (new features), there is no time to document things in this way.
Re:There’s No Such Thing as Clean Code by qayxc on Friday January 28, 2022 @06:24AM (#30113301)
Jump ship then.

This sounds like you're travelling towards burnout at supersonic speed.

Rapidly changing environment/features is just newspeak for customer has no idea what they even want in the first place and project management is non-existent.

Re:There’s No Such Thing as Clean Code by dt3ft on Friday January 28, 2022 @06:38AM (#30113403)
I agree and did jump ship. This was in Switzerland, by the way.
Re:There’s No Such Thing as Clean Code by dtech on Friday January 28, 2022 @08:17AM (#30114179)
OK, but that doesn't make it right or good advice. It's just one type of of technical debt due to too much workload.
Re:There’s No Such Thing as Clean Code by karottenreibe on Friday January 28, 2022 @04:27AM (#30112488)
In my experience you absolutely SHOULD document every request and removal in e.g. an issue tracker. It's very valuable information.

And commented out code gets stale so quickly. Much better to check out an old version where it still worked than uncommenting a bunch of lines that now have compile errors and have become incomprehensible. Happens so easily, e.g. after some variable renames.

Re:There’s No Such Thing as Clean Code by cjfd on Friday January 28, 2022 @04:50AM (#30112630)
Yes, sometimes (not often) commented out code can be defended. The point of adding why it is commented out is very well taken. Then you can remove it if the reason for it still being there is not longer applicable. And commented out code tends to go stale quite quickly so if it really is not needed anymore it should be removed.

But I do wonder, if the better option would not have been to make the behavior that you commented out optional. Sometimes one can know beforehand whether a request is actually what people are going to want in the end. Once I wrote variant 1 of a certain feature. This was deemed not good and variant 2 was written. This was also not what people wanted so variant 3 was written. By then I got the inkling that also variant 3 would not be there to stay so I made it an option whether 2 or 3 was in effect. In the end, the final functionality was mostly a lot like variant 1 so the option turned out not be useful but maybe sometimes such optional behavior can sometimes be the best way....

Re:There’s No Such Thing as Clean Code by D-Coder on Friday January 28, 2022 @02:03PM (#30118919)
> we had frequent "please bring it back" requests that came a few months after things were removed (also by request)

Not exactly relevant to this discussion, but it sounds like a good case for a config flag. Let them turn it on and off as often as they like.

Re:There’s No Such Thing as Clean Code by Siira on Friday January 28, 2022 @12:14PM (#30117493)
I have started to prefix my actual comments with another character after the comment character, e.g., #: in Python. Then I know which comments are “real,” and which are just garbage code that can safely be ignored/deleted.
Re:There’s No Such Thing as Clean Code by cerved on Friday January 28, 2022 @06:50AM (#30113494)

  git log -p -- file
Re:There’s No Such Thing as Clean Code by GordonS on Friday January 28, 2022 @04:40AM (#30112583)
> If you follow them you will end up with code that's really nice to read and easier to maintain, and, most importantly, that you can confidently change

...for trivial cases. For more complex, real-world scenarios, these kind of rules need to be applied sparingly, and where needed. Otherwise you get things like too much abstraction and several layers of inheritance, making it difficult to figure out what anything actually does - the opposite of nice to read

Uncle Bob et al have some good guidance, but cargo-culting them led to long-lasting, negative effects on some communities, such as Java and C# developers.

Re:There’s No Such Thing as Clean Code by bobcostas55 on Friday January 28, 2022 @04:21AM (#30112446)
If you follow them you will end up with an unreadable spaghetti made out of thousands of two-line functions.
Re:There’s No Such Thing as Clean Code by hbbio on Friday January 28, 2022 @04:14AM (#30112394)
Most items are great but I was unsure about "one assert per test" until I read the comment below:

<< I would change "one assert per test" to "one concept per test". Uncle Bob makes a fairly big deal about how the former is a misconception. >>

Re:There’s No Such Thing as Clean Code by otikik on Friday January 28, 2022 @04:29AM (#30112502)
On the internet I have very rarely encountered "Clean Code" as code following explicitly the rules listed by Bob Martin in his books. People use it like the OP says - "code that feels good to me in the moment, without being able to explain why".

I believe that's because the term Clean can be intuitively defined in opposition of quick and dirty : "I have spent a lot time on it. So it isn't quick and dirty. It's clean."

Re:There’s No Such Thing as Clean Code by asimpletune on Friday January 28, 2022 @05:03AM (#30112714)
I'm all for the following the spirit of things, but I also want to take a minute to disagree with the "without being able to explain why" part.

I have worked with people who were super booked up on Go4 and all the design stuff, and yet they can be fairly insufferable to work with when they parrot design advice from all this stuff, precisely because they can't explain why in every given situation. Then, on the receiving end of things, most of the criticism people who are expressing in this forum wrt to uncle bob, do so about examples where they coming from the perspective of something they understand.

I think there is A LOT of value in all of this experience, but it has 0 utility if you don't understand it. In other words, you are much better off not considering how you feel about code at all, and always sticking to doing things because you understand why. (And I'd say 'understand' means in a way that you can explain to someone else who may be skeptical). Context is key... so it's like if you're about to do a live coding demo in front of the board, then yeah, it's ok to comment stuff out - I think that's obvious. Ideally, your outcomes will be much, much better if they're coming from those 'obvious' points of view in any given situation.

Any just to be fair, doing something because you're curious, or it's fun, is just a good a reason as any, it just depends on the context.

This is why, there are other people who I've worked with, who know all that stuff - I'm talking super senior people - and yet rarely ever cite it or even mention it. Usually, they will ask questions because they're patiently trying to get you (or whomever) to see for yourself the same, all-encompassing 'obvious' reason, that overrides all the other important factors in a given context, that they already see. If you ask them why a piece of code that doesn't 'feel right' to you is the way it is, they'll usually have a story about some situation, where they just had to do it that way, and maybe it's bad now but if you were there, and knew what they knew, you would have probably done the same.

So, my point is, yes uncle bob stuff is fairly sound, but no I don't agree that we should follow it if we don't understand it and we're better off almost always sticking to what we do understand. O but also cool analogy about the quick and dirty. I just realized though that if you invert it completely it's like 'slow and clean'.

Re:There’s No Such Thing as Clean Code by davedx on Friday January 28, 2022 @03:40AM (#30112180)
Yeah I agree with almost all of these. IMO the most important is “keep it simple”. That is always always my coding North Star and it’s served me well over the many years I’ve been programming.
Re:There’s No Such Thing as Clean Code by omegalulw on Friday January 28, 2022 @03:46AM (#30112206)
+1 when I write code I always think about how my reviewers would perceive it before sending it over and I refactor based on that. You should aim to write code that's easy for others to follow.
Re:There’s No Such Thing as Clean Code by raverbashing on Friday January 28, 2022 @03:41AM (#30112188)
> is a set of rules that absolutely are not at odds with one another

Yes, if you take them as general guidelines, not dogmatic rules with variable interpretations

The latter is exactly what his fans do

> 2 .Keep it simple stupid

> 2. Prefer polymorphism to if/else or switch/case.

No, not at odds with one another...

Re:There’s No Such Thing as Clean Code by beebmam on Friday January 28, 2022 @03:46AM (#30112208)
Yeah, polymorphism is in no way simple in lots of languages/frameworks. It's actually quite complex in many common languages/frameworks. This is absolutely contradictory in my opinion, but I welcome disagreement :)
Re:There’s No Such Thing as Clean Code by mcv on Friday January 28, 2022 @04:51AM (#30112631)
And then there's a language like Scala where switch/case is so naturally integrated that it actually becomes the cleaner solution and you basically have to use it.

Whatever approach is clean or not can vary wildly by language. Or framework.

I do like clean code, but I think the article has a point: it's a very vague concept.

Re:There’s No Such Thing as Clean Code by hyperman1 on Friday January 28, 2022 @04:11AM (#30112373)
I see polymorphism vs if/... more as a tradeoff. For example:

Suppose the program has to make a choice. When the choice exists only in 1 place, use an if/switch. When the choice exists in multiple places, polymorphism centralizes it and quickly becomes the simple option.

For example, a change in law required us to make a choice based on a date:

   If (thing.someBusinessDate < 2010-06-01) { doTheOldWay();} else{doTheNewWay();}
after a while, a few edge cases sprang up, and we got variants of this code hiding in multiple corners. Then the law changed again with a new cutoff date. So we adapted to polymorphism:

  interface TheWay{ edgecase1(); edgecase2();...}    
  class TheOldWay implements TheWay{...}
  class TheNewWay implements TheWay{...}
  class TheNewNewWay implements TheWay{...}
  TheWay wayDetector(thing){/* all the ifs here */}

Unfortunately, adding just one more 'if' was always the cheapest choice. The refactoring was nevertheless clearly a better choice: it enabled us to point out more forgotten edge cases, avoiding costly mistakes.
Re:There’s No Such Thing as Clean Code by viceroyalbean on Friday January 28, 2022 @04:24AM (#30112464)
I agree with you, especially in instances where that decision logic is unrelated to the data that is actually being used. In your example you'd have to pass "thing" everywhere just for that if-statement, but it might not be used for anything else.

That said, I think "simple" if/else or switches are much more common than the complex cases that fit polymorphism, so it seems a bit off to have it as a general rule to prefer polymorphism.

Re:There’s No Such Thing as Clean Code by juped on Friday January 28, 2022 @03:48AM (#30112215)
They aren't. This is why pattern matching and typeclasses (to use the Haskell words, though I know other words are in use as well) have become increasingly popular features in languages.
Re:There’s No Such Thing as Clean Code by onion2k on Friday January 28, 2022 @03:59AM (#30112289)
Polymorphism is more complex than a switch/case in a single instance. Across a large codebase using polymorphism instead of hundreds of different switch/cases is simpler, especially when you need to change something. Encapsulating what a class should do within the class instead of in an external code block is much simpler when you're working on something big.
Re:There’s No Such Thing as Clean Code by fpoling on Friday January 28, 2022 @04:24AM (#30112469)
Polymorphism is a switch, but it is the switch over object kind, not action. In fact if there are only few subclasses compilers for some languages where a global analysis is possible implement polymorphic calls as switches over the object tag.

And notice how if one needs to add a new action, all objects are required to implement it. In typical OOP languages that touches many files even if the action is used only once.

So polymorphism works nicely if there are only few actions but many different objects, like in a typical GUI. But in cases of few objects and many actions, for example, doing a lot of different queries over few data sets it leads to a lot of boilerplate. Switch over actions will lead to less code.

Re:There’s No Such Thing as Clean Code by mpweiher on Friday January 28, 2022 @03:49AM (#30112218)
Hmm...polymorphism is almost invariably simpler than if/else or switch/case, so not seeing the contradiction.
Re:There’s No Such Thing as Clean Code by ajuc on Friday January 28, 2022 @03:56AM (#30112272)
I've seen this happening a lot:

- you have the same if-then-else in 3 places in code with 3 different effects

- you refactor it to polimorphism (Enum with a few overloaded methods works well in java)

- now one of these places need additional condition so you make it a little more complex

- year passes

- you have eldritch abomination of OOP instead of 3 if-then-elses with slightly different conditions because at no point someone was brave enough to refactor back into if-then-elses as the special cases piled up

Re:There’s No Such Thing as Clean Code by ziml77 on Friday January 28, 2022 @09:51AM (#30115287)
Exactly my experience with polymorphism. It starts out looking super clean but then the business requirements shift and each has its own edge cases that turn it into a giant mess, often ending up as a mix of both polymorphism and convoluted if statements.
Re:There’s No Such Thing as Clean Code by fortylove on Friday January 28, 2022 @03:54AM (#30112261)
Is that view not subjective though? Might someone think that if/else is simpler?
Re:There’s No Such Thing as Clean Code by qayxc on Friday January 28, 2022 @06:33AM (#30113373)
Depends entirely on the metric used.

Cyclomatic complexity would put if/else behind polymorphism while using lines of code and depth of inheritance would favour the if/else way.

That's the crux with software - there's not always an objectively better solution in terms readability/comprehensibility and some metrics that try to capture these qualities are inherently at odds with each other.

Re:There’s No Such Thing as Clean Code by rk06 on Friday January 28, 2022 @03:59AM (#30112294)
Polymorphism can replace multiple if/else/switch/case with a single one.

It has benefits, if you do not apply it dogmatically everywhere

Re:There’s No Such Thing as Clean Code by bsza on Friday January 28, 2022 @04:26AM (#30112481)
> Don't use flag arguments. Split method into several independent methods that can be called from the client without the flag.

vs

> Needless Repetition.

Re:There’s No Such Thing as Clean Code by lmm on Friday January 28, 2022 @04:33AM (#30112540)
Look more carefully, there's no contradiction. Splitting a method into several independent methods doesn't mean you repeat yourself.
Re:There’s No Such Thing as Clean Code by bsza on Friday January 28, 2022 @04:40AM (#30112584)
The flag check could be deep within a loop, how would you split it then without repeating the whole method?
Re:There’s No Such Thing as Clean Code by lmm on Friday January 28, 2022 @05:47AM (#30113021)
Make the loop operation something that's passed into the method. Or maybe replace the flag with polymorphism if there's some logical value that it really belongs on.
Re:There’s No Such Thing as Clean Code by bsza on Friday January 28, 2022 @06:25AM (#30113316)
The second one is impractical because the number of classes you'd need would increase exponentially with each added flag. The first one clashes with

> Prevent over-configurability,

because you're accepting an infinite range of possible callback methods instead of asking a simple yes/no question.

I don't get why this rule even exists, actually. If you take three boolean arguments, that's not okay, but if you wrap them all into a configuration object and pass that instead, that's suddenly... okay?

Re:There’s No Such Thing as Clean Code by lmm on Friday January 28, 2022 @07:43AM (#30113872)
> the number of classes you'd need would increase exponentially with each added flag.

Only if you actually call it exponentially many different ways, which seems unlikely.

> I don't get why this rule even exists, actually. If you take three boolean arguments, that's not okay, but if you wrap them all into a configuration object and pass that instead, that's suddenly... okay?

Three or four booleans are not something semantically meaningful that you can reason about. Five or six classes representing the cases that actually occur in your program, that's something you can understand. Often passing a callback function is too.

Boolean flags fall into a hole in the middle somehow, probably because of the exponentially many possible combinations; they look like a finite set of possibilities, but in practice you almost certainly haven't tested all the cases and a lot of them probably don't make sense.

Re:There’s No Such Thing as Clean Code by asimpletune on Friday January 28, 2022 @05:07AM (#30112745)
Using map?
Re:There’s No Such Thing as Clean Code by throwawayffffas on Friday January 28, 2022 @04:26AM (#30112483)
> that you can confidently change.

There is no secret to this, it's merely tests, no matter how clean my code is I would not feel confident unless I have tested my changes, whether manually or automatically and manually quickly becomes intractable.

There is the option of formal verification, but I have found the tooling to be disagreeable with me.

Re:There’s No Such Thing as Clean Code by onion2k on Friday January 28, 2022 @10:25AM (#30115735)
If your code is complex you can't ever be sure that you're testing the right things, or that there isn't a codepath that your tests miss. Tests are very useful, but they're not a replacement for simple code.
Re:There’s No Such Thing as Clean Code by jeremy8883 on Friday January 28, 2022 @10:09PM (#30123412)
Bob himself has mentioned that many of the rules are contradictory to one another. I can't remember what examples he gave. I think it was in his video series.