Live data from Hacker News

There’s No Such Thing as Clean Code

steveonstuff.com

41–50 of 395 posts

Re: There’s No Such Thing as Clean Code

#41

> But these traits are in some ways at odds with each other. The most simple code is probably not the most testable. All those interfaces and injected dependencies make for convenient testing, but have a cost in terms of simplicity. Exactly The same code snippet might be good in one context and an annoyance in another context Here's where "generic rules" fail. Like python's avoidance of lambdas and favouring just hav…

20 short functions definitely sound as though they should be explicit. Named, documented, testable. 1 or 2 you could get away with being implicit. 20 requires a lot of understanding as to what's going on!

So here's some TypeScript code I just made up, with a lot of lambdas. It's somewhat typical of code I write all the time.

    books
        .join(authors, book => book.author, author => author.id)
        .filter(([book, author]) => author.lastName === searchText)
        .map((book, author) => `The Book ${book.title}, by ${author.fullName()}, has ${book.chapters.count()} chapters, totaling ${book.chapters.sum(chapter => chapter.pages.count())} pages.`);
There are 5 lambda functions in there. Can you tell what the code is doing? Is it correct? Yes, and yes. This is the kind of code that, in my opinion, doesn't need tests at all, nor comments. You can look at it and understand what it's doing and know that it is doing it correctly, as long as it compiles. If it's mission critical, you should test that the join really should be on book.author and author.id, but you need to know the correct answer to write the test, so why not just look at the code and verify it's correct? If your answer is "because another change might break it": no, it won't! Given the preconditions, that code is correct, and no other code can effectively break (and still have your code compile) it without lying to the Type-checker. If someone breaks that code, it's because they're intentionally changing it to do something else, so they'd redline any tests you wrote for it anyway.

It sounds like you're suggesting this code should be more like:

    /// 
    ///    Gets the author of a book
    ///    The author of the given book
    ///   
    ///       The book to get the author of
    ///   
    /// 
    function getAuthorOfBook(book: Book) {
        return book.author;
    }

    @testMethod()
    function canGetAuthorOfBook() {
        const mockBook: Book = {
          author: 'Test Author',
          title: 'Verbosity',
          ...
        };
        
        Assert.areEqual('Test Author', getAuthorOfBook(mockBook);
    }
...

    /// 
    ///     Takes a last name and returns a function that returns true if the given (Book, Author) tuple contains an author whose last name matches the given string of the outer function.
    /// ...
    function getBookAuthorPairPredicateFromAuthorLastName(lastName: string) {
        return function(pair: [Book, Author]) 
            return pair[1].lastName === lastName;
        }
    }
And on and on and on, still needing the original code, but just a lot more obfuscated:

    books
        .join(authors, getAuthorOfBook, author => getIdOfAuthor)
        .filter(getBookAuthorPairPredicateFromAuthorLastName (searchText))
        .map(describeBookAuthorPair)
Now I have no idea what the hell this code is doing and whether it's correct or not. Note that to avoid lambdas in the filter (filtering by a value in the closure) we need to write a function that returns the predicate we want to test on, instead of just sticking the right thing in the right place to start with. All to avoid a single "=>".

What circle of hell are we in!?

Re: There’s No Such Thing as Clean Code

#42
> There’s no such thing as clean code. > ‘Clean’ isn’t a measure of anything useful. Code can’t be clean simply because ‘clean’ doesn’t describe anything about code.

Ehhhh. There is absolutely such a thing as clean code. But yes; what there isn't a way to measure code cleanliness (although there's lots of surrogate measures; see every linter) which means there's also no way to render it into a dogma...

...and to (perhaps) stick my foot in it, that's something that gets harder to grok the less important subjective human experience is to you. IMHO, this is part of why Ruby code has an easier time being cleaner (and can reach high levels of "clean") - subjective human experience is baked into the language.

(note: yes, you can absolutely write dumpster fires in Ruby, arguably easier than you can in Java; and you can absolutely write very clean code on Java and any other language)

> I’ve come to the conclusion that often when we describe code as ‘clean’ when we think it’s good but we’re not entirely sure why. It just feels like the right solution.

Yes, absolutely. There's a lot of things in popular wisdom that are signposts for "there's more here if you pay attention".

> absolve you from having to justify that with more concrete rationale.

...Why in the hell do you need absolution here in the first place?! What are you being absolved from?

(shameless quote: justice only matters to the just)

> you don’t really need clean code, you need _____ code

No. That's the trap of Goodhart's Law. You need something outside of your metrics so that your metrics don't become your target.

Re: There’s No Such Thing as Clean Code

#43
post #29

Clean code is code that does what you expect it to do without many surprises. It is simple, not clever. Effortless to follow. Each part handles one idea at a time, at the same abstraction level. Doesn't force you to mentally juggle many balls at the same time. The code often tells you a story, it communicates how the programmer (author) described the problem, the solutions and the trade-offs. Very similar to writing.…

In my work history, I've never come across code like this, especially "effortless to follow". All the codebases I've worked with have been head scratch causing balls of mud. Am I unlucky or is what you are describing the rare exception?

Most large software projects have some bad code in it. If it’s all bad, then there is probably an architectural problem, poor code review practices, and/or corners are being cut to meet deadlines.

Re: There’s No Such Thing as Clean Code

#44
post #29

Clean code is code that does what you expect it to do without many surprises. It is simple, not clever. Effortless to follow. Each part handles one idea at a time, at the same abstraction level. Doesn't force you to mentally juggle many balls at the same time. The code often tells you a story, it communicates how the programmer (author) described the problem, the solutions and the trade-offs. Very similar to writing.…

In my work history, I've never come across code like this, especially "effortless to follow". All the codebases I've worked with have been head scratch causing balls of mud. Am I unlucky or is what you are describing the rare exception?

Both. Head-scratching balls of mud is, IMO, below average; but that lofty height of code described is an extreme rarity.

100% recommend having your own hobby project so you can reach it.

Re: There’s No Such Thing as Clean Code

#45
The fact that everyone can come up with his own definition of what "clean" is supposed to mean regarding code, tells us something very important about it:

It has no intrinsic, defined meaning in the context of code.

Saying code is "clean" is like saying food is "tasty"...its a personal opinion, not a defined term.

Re: There’s No Such Thing as Clean Code

#46
post #29

Clean code is code that does what you expect it to do without many surprises. It is simple, not clever. Effortless to follow. Each part handles one idea at a time, at the same abstraction level. Doesn't force you to mentally juggle many balls at the same time. The code often tells you a story, it communicates how the programmer (author) described the problem, the solutions and the trade-offs. Very similar to writing.…

>> It is simple, not clever. Effortless to follow. This is the crux of it for me. I want to read code not solve code. If I have to "figure out what's going on" then it's not great code.

YES. An older Rubyist once told me: "Code is programmers communicating with other programmers."

Re: There’s No Such Thing as Clean Code

#47
post #29

Clean code is code that does what you expect it to do without many surprises. It is simple, not clever. Effortless to follow. Each part handles one idea at a time, at the same abstraction level. Doesn't force you to mentally juggle many balls at the same time. The code often tells you a story, it communicates how the programmer (author) described the problem, the solutions and the trade-offs. Very similar to writing.…

In my work history, I've never come across code like this, especially "effortless to follow". All the codebases I've worked with have been head scratch causing balls of mud. Am I unlucky or is what you are describing the rare exception?

There definitely are codebases that are easy to follow.

But it requires a team that is committed to aggressively refactoring even the smallest of code smells, usually before even committing it. It also requires a team that is dedicated to its professionalism and not bend into a manager's will of refactoring being a time waste.

But when your team is committed to code quality, holy hell is it satisfying, easy and fast to work with. It really is like night and day. If you have not experienced the difference - I'm sorry to say - then you've just not worked with a high-quality team.

Re: There’s No Such Thing as Clean Code

#48
post #29

Clean code is code that does what you expect it to do without many surprises. It is simple, not clever. Effortless to follow. Each part handles one idea at a time, at the same abstraction level. Doesn't force you to mentally juggle many balls at the same time. The code often tells you a story, it communicates how the programmer (author) described the problem, the solutions and the trade-offs. Very similar to writing.…

In my work history, I've never come across code like this, especially "effortless to follow". All the codebases I've worked with have been head scratch causing balls of mud. Am I unlucky or is what you are describing the rare exception?

Same even for code base my only job was to cleanup, it would be clean 1 week then split back into mess, sometimes by my own doing. The problem is always the same: there is money for result and result is time-sensitive, no money for form and form takes time. If I have to sacrifice a bit of form to reach a result on time, I'm afraid I'll do it rather than fire a colleague because we can't pay him and do beautiful code :s

Re: There’s No Such Thing as Clean Code

#49
post #29

Clean code is code that does what you expect it to do without many surprises. It is simple, not clever. Effortless to follow. Each part handles one idea at a time, at the same abstraction level. Doesn't force you to mentally juggle many balls at the same time. The code often tells you a story, it communicates how the programmer (author) described the problem, the solutions and the trade-offs. Very similar to writing.…

>> It is simple, not clever. Effortless to follow. This is the crux of it for me. I want to read code not solve code. If I have to "figure out what's going on" then it's not great code.

Exactly. “Write code for humans first”.

Re: There’s No Such Thing as Clean Code

#50
From the list at the link, I would argue that the following are mostly orthogonal of, and certainly not requirements from, clean code:

1. Performant

2. Safe

3. Scaleable

4. Easy to delete

---

Performant: I can write you a nice clean bubble sort implementation, which will obviously not be performant.

Safe: You could write a nice clean server which reads input from a network socket and executes it via `system(...)`. This would not be safe.

Scaleable: That's not even properly defined. How would you "scale" my device driver? Or my microcontroller logic? ... does that mean those can never be clean?

Easy to delete: To delete and replace? To delete for building tests for other parts of the code? Didn't even get that.

Post reply on HN