| ▲ | Beyond "Clean Code": Why Your Comments Matter(blog.moertel.com) | |||||||||||||
| 23 points by chmaynard 2 days ago | 10 comments | ||||||||||||||
| ▲ | johnmwilkinson a day ago | parent | next [-] | |||||||||||||
I completely agree. I wrote a book on the subject of how to write clear code (Bob Martin was off by one letter!) and comments are a key element of that. https://elementsofcode.io/chapters/documentation/ We’re in good company: Jon Ousterhaut had a conversation with Uncle Bob where he brings up the exact passage you quote as a point of disagreement. | ||||||||||||||
| ▲ | 4xel 21 hours ago | parent | prev | next [-] | |||||||||||||
While I agree with the conclusion, I don't agree with all the examples given. > Invariant: From this point on the array’s length must be 3N + 1 for some integer N. Invariant can be checked by code, and they definitely should if your language allows constraints or asserts (checked at comptime or ran in test and debug mode) > We use simple brute force here because instances are guaranteed to be small (see guard above). The guard above probably conveys the information already. > Limit each request to one day’s data; otherwise, we won’t get 1-minute resolution. This could be a benchmark, though I wouldn't be unhappy to read this as a comment, and I wouldn't care if there was no benchmark. A couple others could be in a separate technical doc rather than in comment. I'm on the fence about it, a bit more on your side, but there are ways and places to express intent outside of the source code that the "no comments" crowd may argue for. As for the python example, it's a great illustration of your point, there are many comments, but it's hard to remove any. Still: # Weights must be non-negative integers. and # The distribution must have a positive total weight. Can be discarded without losing anything: the parameter validation they comment is already transparent, and even includes natural language in the error message, describing what happens why. Some other comments describe the what rather than the why, but the code it refers to is dense enough that it's warranted. | ||||||||||||||
| ||||||||||||||
| ▲ | magicalhippo a day ago | parent | prev | next [-] | |||||||||||||
I reached the same conclusion. Yes, absolutely let the code be self-documenting by structuring it well, using well-named functions and variables and so on. This will make it clear what you're trying to do. So yes, you shouldn't write "check if greater than two" in a comment, that should be obvious from the code itself. But sometimes the why is very important, and it is not readily obvious from the what. Ideally try to modify the code so it becomes clear. Don't just check against two, check against a named constant that provides information through the name. However what can be more difficult to express cleanly is for example why the check is being done here and not somewhere else, in the cases where that matters. In those cases, help the reader by adding some clarifying comments as to why this is being done. | ||||||||||||||
| ▲ | wduquette 20 hours ago | parent | prev | next [-] | |||||||||||||
I'm mostly a solo programmer, and I often have to go back and look at code I wrote five, ten, twenty years ago. And every time I do, I give thanks that I used comments to document my intent. My memory's good, but it's not that good. | ||||||||||||||
| ▲ | mrozbarry a day ago | parent | prev | next [-] | |||||||||||||
I think Bob Martin would generally agree. I think his gripe was using comments to make your code more readable, and that's a problem. Variable and function naming should make the code logic clear and readable. When it comes to feature requirements/explanation, that's a different thing, and a hard problem. Documenting features in code means it's only available to coders. Duplicating feature documentation from some external source to code means it risks becoming out-dated. Syncing tickets, bug fixes, code, and feature details is a hard problem. I'd generally prefer doc-block style comments that link to feature/product documentation, which is good for humans, and probably LLMs, too. Wherever you put it, keep it consistent and searchable. | ||||||||||||||
| ||||||||||||||
| ▲ | blinkbat 2 days ago | parent | prev [-] | |||||||||||||
I disagree that it's the codebase's job to communicate product's intent | ||||||||||||||
| ||||||||||||||