News for hackers. Stuff that matters.
Re:On Comments in Code by benrbray on Wednesday June 16, 2021 @12:44AM (#27525004)
Tests are useful, but the big reason that comments are useful is that they are co-located with the relevant code. At the moment, I don't know of any tools that allow you to 1) specify which lines of code are directly relevant to a particular test case, even when the surrounding code changes and 2) see a list of test cases related to the currently highlighted line.

I'm sure it would be possible to build such a tool, but the complexity is quite substantial compared to a simple comment or two. If you really want to go the full-test-coverage route it makes a lot more sense to focus that energy on pure/immutable/statically-verified programming rather than some finicky test cases that add substantial maintenance cost to the code.

Re:On Comments in Code by OJFord on Wednesday June 16, 2021 @07:12AM (#27527158)
> Tests are useful, but the big reason that comments are useful is that they are co-located with the relevant code.

That's a not-often-talked-about (that I see anyway?) thing I really like about rust - I don't get to use it much, but when I do I think having 'doc tests' alongside comments heading a function and 'module tests' at the bottom of the same file is great.

Re:On Comments in Code by dstick on Wednesday June 16, 2021 @01:31AM (#27525226)
Exactly. And then the question remains: why? Why hate comments so much? I never got that
Re:On Comments in Code by rwallace on Wednesday June 16, 2021 @06:42AM (#27526964)
I would conjecture that it has two components:

1. Ancestral memory of programming in languages like assembly or early Fortran where your code was going to be spaghetti because the language was capable of little else, so it had to be drenched in comments that would be unnecessary in a better language.

2. Overreaction to personal memory of overzealous professors who demanded assignments be drenched in excessive comments.

Re:On Comments in Code by benrbray on Wednesday June 16, 2021 @08:37AM (#27527798)
Related to #2, I'm still recovering from high school AP English. We had to meet quotas for a certain number of underlines / notes written in the margin per page. For the better part of a decade I haven't been able to pick up fiction without the feeling that I'm working through a textbook.
Re:On Comments in Code by cush on Wednesday June 16, 2021 @01:55AM (#27525343)
Until the code changes, then the comments are usually worse than nothing
Re:On Comments in Code by benrbray on Wednesday June 16, 2021 @04:24AM (#27526176)
No. Comments are like a parity check. If the comments don't match the code, then you know that either:

1) The comments are correct, but the code is wrong. This is a bug. 2) The comments are wrong, but the code is correct. This is equally a bug. Whoever makes the change has an obligation to describe their changes with a comment.

Code with no comments? Could be right, could be wrong, who knows?

Re:On Comments in Code by cush on Tuesday June 29, 2021 @09:45PM (#27683986)
Good luck maintaining that.
Re:On Comments in Code by ghoward on Thursday June 17, 2021 @04:23PM (#27544148)
I wish I could upvote your comment more, but putting it in my bookmarks will have to do for now.
Re:On Comments in Code by AlotOfReading on Wednesday June 16, 2021 @03:38AM (#27525921)
I've seen this counterpoint brought up many times before, but I'm struggling to think of the last time I experienced it. Is it really a common problem for you?
Re:On Comments in Code by cush on Tuesday June 29, 2021 @09:49PM (#27684007)
Yes, the issue isn't that most comments are inaccurate, it's that the inaccurate ones are so costly. When the comment doesn't match the code, it makes you question the code and the intent. 90% of the time, comments are accurate or just redundant (the code is descriptive enough), but that 10% of the time when the comments says A and B is happening, I end up chasing down whoever or digging through changelogs and tickets to find the comment wasn't updated.
Re:On Comments in Code by b3morales on Wednesday June 16, 2021 @08:07PM (#27534887)
For the kind of code comment we started this thread talking about (explaining why some simpler/more obvious process is not used), this seems vanishingly unlikely.
Re:On Comments in Code by taneq on Wednesday June 16, 2021 @04:24AM (#27526175)
Then you fire the coder who changed the code without updating the comments?
Re:On Comments in Code by cush on Tuesday June 29, 2021 @09:50PM (#27684012)
Most code doesn't need comments.
Re:On Comments in Code by kwhitefoot on Wednesday June 16, 2021 @05:49AM (#27526671)
That's a waste. Just train them better.