19

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?

1
  • 2
    What about the trivial error actually being a part of the OP's problem? This hasn't been addressed in the answers, just wanted to bring it up.
    – 11684
    Commented May 1, 2014 at 16:24

4 Answers 4

17

In my opinion, if you are changing the logic of 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.

2
  • 2
    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].
    – supercat
    Commented Dec 14, 2014 at 21:31
  • Note: authors are always notified of any edit on their post.
    – Braiam
    Commented Jan 21, 2017 at 16:04
11

If it's something that simple, edit then leave a comment explaining what you did. Something along the lines of:

I just spotted what looks like a typo in your code - hope my correction is OK.

The answerer will get notified of your edit suggestion anyway, but the comment is a friendly way of giving more information.

Also make it clear in the edit summary why you are making the specific edit. Then those reviewing the edit can have all the information at hand to (hopefully) make the correct call.

If they really did mean the original code they can always roll back.

For more complex situations an initial comment is the way to go.

3

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.

-2

Better highlight it in the comments imho. Else you run a very real risk of seeing your edit rejected as invalid by a robo-reviewer.

Once you've reached the reputation level needed to edit stuff without it getting reviewed, edit to your heart's content.

2
  • 2
    "But don't forget to leave a comment stating you did so." I just edited your post -- annoying, isn't it? 😄
    – Jongware
    Commented Apr 30, 2014 at 21:25
  • @Jongware: there's a notification for that. Commented Apr 30, 2014 at 22:29

Not the answer you're looking for? Browse other questions tagged .