I know that it's wrong to edit code in a question/answer, as discussed in "When is it appropriate to edit someone else's code?" and "How far can I refactor someone else's code?". Instead of editing the code it is suggested to "leave a comment and let the author correct it".

I also know that it's wrong to post duplicate answers: basically the same code as another answer but with a few fixes (real fixes like memory leaks, not typos) and possibly another description of the code/solution.

My question is:

Given that I have already commented the suggested changes on an answer. Is it better to post the same answer but modified or is it better to edit the original answer or should I really do neither and just forget about it (after commenting).


I'm seeing certain bad practices and misunderstood code in both questions and answers and would like to reduce the risk of these spreading by people finding these answers and copying code. Not everyone reads through all the comments.

Of course I'm intending to substantially improve the answer and not just copy paste the whole thing. One example is this answer, thought it's only a few hours old so there is still a change that the answerer will edit it.

In the above example I would rewrite the code to do proper day calculations (adding seconds is a really bad practice) and not leak the first date object (I've seen many people first create a new object and then assign something else to that variable on the next line) and explain why the other change makes the code work and not just post a bunch of code without a discussion.

3 Answers 3


Such substantial changes shouldn't be made in an edit. An edit should be improving the post while maintaining the same content as the author.

It would certainly be appropriate to add another answer, even if you've commented on another answer to indicate problems that it has. Note that if you use a portion of the contribution of another answerer in your answer (even if you've changed it to some degree) you should cite the material that you've used to ensure it's properly attributed.

Also note that if you feel strongly that this other answer is problematic and would be harmful to other readers I strongly encourage you to downvote the answer; that's the intended mechanism for conveying such information.


The response is different whether you want to fix code in a question or an answer. They are entirely different.

For a question, there are three possibilities:

  • you see an error in their code that is causing the thing they are asking about. You just found the answer to their question. If you see an answer that already says that, vote it up. Otherwise, answer the question.
  • you see an another error that hasn't hurt them yet, but that will when they fix their main problem. (Eg the question they are asking is "Why won't this compile?" and someone has already told them that they have a problem on line 10, but you can see something on line 11 that will cause an error at runtime.) Add a comment on the question. Alternatively, if you are writing your own answer anyway (because no-one has answered the question as asked), toss in a "ps" or "btw" about the other problem. Do not add an answer of your own only to make this distinction.
  • you see an error in their code that is not obvious (some might even say a matter of style, preference or practices) and is not what they asked about. (Eg sql injection vulnerability, keeping passwords in plain text rather than hashed, buffer overrun vulnerability, functions too long, too much code duplication, not internationalizable, using a library you think is inferior etc). As before, a comment on the question or a paragraph in your own answer, but include a link to another question, a blog, or a reliable web page explaining it. Don't try to fit the whole issue into a small space and don't encourage discussion of that issue in place. And don't add an answer that differs from another only by including this information.

For an answer, there are four possibilities:

  • the error is obvious (forgotten semi colon, missing cast, nit where it should be int - add a comment, and after a few days, edit if you have the power. Don't use suggested edits for this unless the answerer commented saying "you're right, thanks" but doesn't edit it. Someone with edit powers may do the edit, or worst case, future readers will have your comment to help them if they run into trouble using the answer.
  • the error is factual (that API takes 4 parameters, not 3) and non controversial, but not an obvious typo. Comment. After a few hours with no reply, add your own answer that does it correctly.
  • the "error" is a difference of style or practice as in the third possibility for a question. Comment, but do not add an answer that does not answer the question. If the question is "how do I make this a parameterized query to avoid SQL injection?" and an answer has been provided that does that, don't write a different answer that does that but hashes the passwords. Instead, give them a comment with a link so they can learn why they should hash the passwords, and perhaps a link to a question and answer that show them how. Otherwise you're hijacking their question.
  • the "error" is that the author didn't comment on flaws in the code provided in the question. You have commented on the question already pointing out these flaws, so leave the answers alone. They are under no obligation to fix all the flaws in the question. They're explaining the null reference exception or the file-not-found error or the compiler warning. Don't edit, comment, or downvote them for not mentioning memory leaks, sql injection or whatnot that was already in the question.

My reasoning here is this: SO is a place to get answers to questions, not a place to make your code perfect in a discussion format. Answers should rarely say "you're doing it wrong" - they should answer the question. Comments can say "you're doing it wrong" and good ones provide links to help the person learn.


I would post a comment, and if author is inactive for a week, or is active and ignores comment for about 3 days, post my reply with correct code and as good explanation as I can get.

You must log in to answer this question.

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