Live data from Hacker News

“My Code is Self-Documenting”

ericholscher.com

11–20 of 100 posts

Re: “My Code is Self-Documenting”

#11
Usually the people who cannot write good code love to write comments because they somehow feel it makes the code better. But they cannot write good comments either. Then everything is horrible. Code is really complex and hard to understand and the comments just make it worse. They might be out of sync or just erroneous.

I'm witnessing this currently with the code base I'm maintaining.

Re: “My Code is Self-Documenting”

#12

The biggest problem I have with comments is that they quickly fall out of sync with the code. The code gets updated but the comments stay the same. Now you have a situation worse than no comments: misleading comments. It sometimes happens with self-documenting code too, but only when someone refactors enough of the code so that the class or method no longer matches the original name.

As others have said..

Code doesn't depend on variable/function names either. Yet you don't argue they fall out of sync, you simply update them. And this should be done for comments, too.

Re: “My Code is Self-Documenting”

#13
> Explaining previous approaches that didn’t work; explaining trade offs in the current implementation; marking possible improvements (TODOs) in the code; anything else you’d like to communicate with someone reading or developing the code

To me, those are the functions of commit messages, not code comments.

Of course, a "proper" IDE (and I don't know of any) would give you the context of the commit messages "impinging upon" any given line/block. Maybe something like Light Table (whatever happened to that?) could be configured to do this.

> Presenting an example usage of the function and example output

And that's what (formalized) docs are for. You can certainly generate those from docstring-style comments, if you like, but personally I feel like they get it the way of editing the code itself, and would be better kept in separate files.

(Consider: would you put the gettext translation table for a string, inline inside the source file that contains said string? The arguments are the same, I think.)

Re: “My Code is Self-Documenting”

#14

I find that the commonly touted opinion that comments should only reflect why, and avoid all duplication; is misguided. The only places where I will elaborate on why is where I can see obvious room for improvement but I'm still waiting for the bigger picture to stabilize, or I'm adding a dependency that I'm not really happy with. Otherwise my comments mostly express the intent of the code in regular prose and have to…

I'd say then that your comments [regarding intent] are overly specific.

If updating the code block results in code with a different intent... you're not updating it, you're replacing it, and should take out the original comment with the original code.

If I have a code block to maintain data synchronization with a a server - that's the intent; if the code is updated from long-polling to websockets, the intent hasn't changed.

Re: “My Code is Self-Documenting”

#15
Self documenting code is not about the "how", it's about the "what". Ex: A method name should be FilterOutOddNumbers(). Not MapModulo2Predicate().

Indeed not everything can be expressed in methods and variable names etc and so comments can be helpful occasionally. But the focus must be on clearly written code with comments as an exception, not a rule.

As for SDC being myopic, I beg to differ. Code (and contents) is for developers and machines. End users and API consumers should have documents (hence documentation) available to them for purposes of tutorials, user guides and references.

Re: “My Code is Self-Documenting”

#16
+1 exactly this.

Aside from "why", or explaining overly clever one-liners, the kind of comments I always find necessary are those providing before/after example of data during a transformation; and example strings w/ resulting capture groups for regular expressions.

Example: # "tag1,tag2,tag3:val" => tags: [tag1, tag2], metadata: [tag3:val] # fun one-liner goes here

Re: “My Code is Self-Documenting”

#17
post #12

The biggest problem I have with comments is that they quickly fall out of sync with the code. The code gets updated but the comments stay the same. Now you have a situation worse than no comments: misleading comments. It sometimes happens with self-documenting code too, but only when someone refactors enough of the code so that the class or method no longer matches the original name.

As others have said.. Code doesn't depend on variable/function names either. Yet you don't argue they fall out of sync, you simply update them. And this should be done for comments, too.

Code does depend on variable/function names. When you change the name at the definition, your code fails to compile until you also change the name at each usage.

This is not true of comments.

If we could make outdated comments produce compilation errors, we would live in a wonderful world :)

Re: “My Code is Self-Documenting”

#18

The biggest problem I have with comments is that they quickly fall out of sync with the code. The code gets updated but the comments stay the same. Now you have a situation worse than no comments: misleading comments. It sometimes happens with self-documenting code too, but only when someone refactors enough of the code so that the class or method no longer matches the original name.

I feel like this translates to "lazy* developers are a problem" rather than "comments are a problems".

Snarkily, how lazy* do you have to be to not even delete comments you make stale?

* And not as in the virtues of programming lazy.

Re: “My Code is Self-Documenting”

#19

The biggest problem I have with comments is that they quickly fall out of sync with the code. The code gets updated but the comments stay the same. Now you have a situation worse than no comments: misleading comments. It sometimes happens with self-documenting code too, but only when someone refactors enough of the code so that the class or method no longer matches the original name.

If you update code without updating the comments and it gets past review you have bigger problems than them being misleading. Good comments are dependent on good developers.

Re: “My Code is Self-Documenting”

#20
I think the Uncle Bob quote is good: "Code comments are not the Schindler's List. They're not pure good."

If a line of code has a comment attached to it, the reader needs to understand both the code and the comment. The comment is written in natural language. There are multiple ways of understanding it. It must be written with great care so that people reading it don't misunderstand it. Otherwise it will likely just confuse people. How about putting that effort in writing the code line with clarity? The reader must read and understand it in any case.

Stick with the code, people!

Post reply on HN