If an otherwise correct and perfectly readable answer writes a simple logical mistake in their code, should I edit it to reflect the answerer's actual intent or comment on the answer, suggesting that they change it?

For example, I just recently commented on an answer that had an and instead of an or in a loop condition.

Obviously something more complex would best be questioned in the comments but if it's as something as simple as the above example, should I avoid cluttering up the comment section and just edit it in myself?

4 Answers
4

In my opinion, if you are changing the code itself, then it needs to be a comment because you can't know for sure what their true intentions were. That way, if the author does not agree, they do not need to rollback, but can comment back why it is incorrect or ignore it entirely.

Even if it perfectly fits their explanation, it just seems better to leave a comment especially if it will be a suggested edit. Suggested edits that change code are usually rejected as an invalid edit since it is hard to tell if it is what the author was trying to achieve especially for those who do not know the coding language in question.

I wish there were a means of attaching a suggested edit to a comment, with the edit being directed to the original poster for review, and allowing the original poster to indicate whether to accept the edit and what sort of credit to give [an OP might regard an edit as helpful, even in some cases where the original code was correct, if the OP takes it as an invitation to make the intention of the posted clearer].
–
supercatDec 14 '14 at 21:31

Changes to code that change the meaning of the code are very rarely minor changes, even if they are only a few characters.

Be certain that it is an actual error in the code, and that your change is an actual fix, and that the fix lines up with what you have good reason to think was the original intention of the author. Then edit away. Yes, you cannot read minds, but the perfect is the enemy of the good.

Include your reasoning why you think it was in error, and why your fix is correct, in the edit comments. This is important -- a raw change to code with no argument why the change is better than what was there before has issues. Expect your change to be possibly rejected by someone who may not even know the language of the code you are changing -- so your argument why your code change is correct needs to be solid.

If you cannot expect to "get back" to the problem if your edit is rejected, also leave a comment, so even if the edit is rejected there will be information for the poor sap trying to work with the problem example code.

A message to the original poster I would stick in the edit message, rather than in a comment: it doesn't add value to 3rd parties, unless said 3rd party is interested in the edit history.