this post was submitted on 04 Oct 2025
73 points (100.0% liked)

Programming

22950 readers
90 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS
 

This is a discussion between John Ousterhout and Martin, who advocated in "Clean Code" to omit comments and split code in extremely small functions. Ousterhout takes that to town by asking Martin to explain an algorithm which Martin presented in his book on "Clean Code", and algorithm that generates a list of prime numbers. It turns out that Martin essentially does not understand his own code because of the way it is written - and even introduces a performance regression!

Ousterhout: Do you agree that there should be comments to explain each of these two issues?

Martin: I agree that the algorithm is subtle. Setting the first prime multiple as the square of the prime was deeply mysterious at first. I had to go on an hour-long bike ride to understand it.

[.. .] The next comment cost me a good 20 minutes of puzzling things out.

[...] I refactored that old algorithm 18 years ago, and I thought all those method and variable names would make my intent clear -- because I understood that algorithm.

[ Martin presents a re-write of the algorithm]

Ousterhout: Unfortunately, this revision of the code creates a serious performance regression: I measured a factor of 3-4x slowdown compared to either of the earlier revisions. The problem is that you changed the processing of a particular candidate from a single loop to two loops (the increaseEach... and candidateIsNot... methods). In the loop from earlier revisions, and in the candidateIsNot method, the loop aborts once the candidate is disqualified (and most candidates are quickly eliminated). However, increaseEach... must examine every entry in primeMultiples. This results in 5-10x as many loop iterations and a 3-4x overall slowdown.

It gets even more hilarious when one considers where Martin has taken from the algorithm, and who designed it originally:

Martin took it from a 1972 publication of Donald E. Knuths seminal article on Literate Programming:

http://www.literateprogramming.com/knuthweb.pdf

In this article, Knuth explains that the source code of a program should be ideally understood as a by-product of an explanation which is directed at humans, explaining reasoning, design, invariants and so on. He presents a system which can automatically extract and assemble program source code from such a text.

Even more interesting, the algorithm was not invented by Knuth himself. It was published in 1970 by Edsger Dijkstra in his "Notes on Structured Programming" (with a second edition in 1972).

In this truly fascinating and timeless text, Dijkstra writes on software design by top-down problem decomposition, proving properties of program modules by analysis, using invariants to compose larger programs from smaller algorithms and design new data types, and so on. Also, how this makes software maintainable. In this, he uses the prime number generation algorithm as an extended example. He stresses multiple times that both architecture and invariants need to be documented on their own, to make the code understandable. (If you want that feeling you are standing on the shoulders of giants, you should read what Dijkstra, Knuth, and also Tony Hoare and Niklaus Wirth wrote).

So, Robert Martin is proven wrong here. He does not even understand, and could not properly maintain, the code from his own book. Nor did he understand that his code is hard to understand for others.

( I would highly recommend Ousterhout's book.)

you are viewing a single comment's thread
view the rest of the comments
[–] Endmaker@ani.social 5 points 1 day ago* (last edited 1 day ago) (1 children)

My take / how I code:

Method length - when in doubt, and there's no time to do much thinking due to a tight deadline, shorter is better

(Method length shouldn't be the determining factor that goes into the design IMO. It should be other principles like cohesion. Shorter methods - on average - just happen to be side-effects of good design)

Comments - generally leave no comment where the code is capable of expressing itself; I do leave comments where it seemed helpful / necessary

Bundling vs TDD - no strong preference; both can be helpful depending on the situation

Bonus: the code for the prime number generator is atrocious. I did not bother reading the sections on it.

[–] HaraldvonBlauzahn@feddit.org 8 points 1 day ago (1 children)

The whole point of software design is that any time invested into it pays back multiple times. "Not having time for it" is usually faulty thinking.

[–] Endmaker@ani.social 8 points 1 day ago* (last edited 1 day ago) (1 children)

"Not having time for it" is usually faulty thinking.

It absolutely is.

The whole point of software design is that any time invested into it pays back multiple times.

Try telling an unreasonable boss this.

[–] HaraldvonBlauzahn@feddit.org 6 points 1 day ago* (last edited 1 day ago) (1 children)

Possibly the reason why Bob Martins seems popular with managers: "Omitting comments makes code shorter. Shorter is simpler and therefore is better. Also, typing in all that comments costs so much time, given that the typing speed of developers in large projects is perhaps 50 lines per day!!".

One can see how that style of thinking leads to auto-generation of source code by LLMs ....

My advice here is in such situations to do whatever increases your respect for yourself. You will see that it does not slow you down.

And one more thing: In 35 years, I have never been fired because I tried to write good code. It's all posturing because in the end, they need you.

[–] sukhmel@programming.dev 3 points 23 hours ago (1 children)

I think my manager is strongly Clean Code inclined. More than once they removed comments, because they will become outdated anyway (so there's no use explaining what is going on at all, right? Right‽)

[–] dandi8@fedia.io 4 points 23 hours ago (2 children)

If you need comments to explain what is happening (and not why it is happening), then you've got some bad code that needs refactoring.

[–] HaraldvonBlauzahn@feddit.org 3 points 22 hours ago

However: Because code can have many layers of abstraction, there can be many layers of "what", too.

[–] sukhmel@programming.dev 1 points 21 hours ago

“Why” comments are of course included in “we don't need that” category