226

The about page says:

Improve posts by editing or commenting

Our goal is to have the best answers to every question, so if you see questions or answers that can be improved, you can edit them.

Use edits to fix mistakes, improve formatting, or clarify the meaning of a post.

The edit page says:

When should I edit posts?

Any time you see a post that needs improvement and are inclined to suggest an edit, you are welcome to do so. The original author of a question or answer may always edit their own post, regardless of reputation level.

Edits are expected to be substantial and to leave the post better than you found it. Common reasons for edits include:

  • To fix grammar and spelling mistakes
  • To clarify the meaning of the post (without changing that meaning)
  • To include additional information only found in comments, so all of the information relevant to the post is contained in one place
  • To correct minor mistakes or add updates as the post ages
  • To add related resources or hyperlinks

When should I edit code in questions and answers?

Resources:

Return to FAQ index

2
  • Clarification request: If the OP's code includes commented out lines, should those be preserved or edited out? It is not included in either Do or Don't.
    – GSerg
    Commented Jan 4, 2020 at 7:47
  • 2
    Use your best judgment, @GSerg. If they might be relevant, then leave them in. If there are a bunch of them, and they're just cluttering things up, then take them out. They will always be available from the revision history, of course. Also...comments on a FAQ aren't a good place to ask a question. There's a very low chance that anyone will see them. Please ask a new Meta question for things like this. We might decide to close your new question as a dupe of the FAQ and update the FAQ, but that doesn't mean it was wrong to ask the question, since it wasn't answered in the FAQ at the time.
    – Cody Gray Mod
    Commented Mar 5, 2020 at 19:57

1 Answer 1

204

Reviewing Code Edits

When in doubt, click 'Skip'. "Learn to love that Skip button."

Editing Code in Questions

Code in questions should only be edited for formatting and readability. Editing the syntax or correcting typos in code in questions can fix the problem that the person asking the question has, causing answers to be unable to address the problem.

Do:

  • Add code blocks around code
  • Add indentation unless the lack of indentation is relevant to the question
  • Add line breaks
  • Fix syntax (non-closed brackets, missing semi-colons, etc.) if you are sure that it is not relevant to the question
  • Fix typos (misspelled function calls, variable names, etc.) if you are sure that it is not relevant to the question

While you're there:

  • Improve the title to match the question content
  • Edit tags to match the question
    • Remember: Tags should help to describe what the question is about, not just what it contains.
  • Fix any spelling/grammar errors outside of the code block

Don't:

  • Change code conventions (delimited_names to camelCase names, etc.)
  • Change the code logic or functionality
  • Fix or change the problem that's being asked about
  • Copy code from a linked site into the question 1, 2
  • Transcribe code from an image to text. It's just too easy to introduce new errors.
  • Ignore improvements that can be made outside the code block (spelling, grammar, etc.)

Example

Original Question:

[PROBLEM] My SQL is foobar (returns nothing)

SELECT TgName, COUNT(*) AS UpVotes FROM Tags INNER JOIN PostTags ON PostTags.TagId = Tags.id INNER JOIN Posts ON Posts.ParentId = PostTags.PostId INNER JOIN Votes ON Votes.PostId = Posts.Id and VoteTypeId = 2 WHERE Posts.OwnerUserId = @UserId GROUP BY TagName ORDER BY UpVotes DESC

Good Edit:

Query Returning No Values

I want my query to return foo, but it is returning nothing instead.

Here is my code:

SELECT
    TgName,
    COUNT(*) AS UpVotes 
FROM Tags
    INNER JOIN PostTags ON PostTags.TagId = Tags.id
    INNER JOIN Posts ON Posts.ParentId = PostTags.PostId
    INNER JOIN Votes ON Votes.PostId = Posts.Id and VoteTypeId = 2
WHERE 
    Posts.OwnerUserId = @UserId
GROUP BY TagName 
ORDER BY UpVotes DESC

Title was made more descriptive, the code was formatted properly, and an explanation of the question was added to the body rather than just having a code dump.

Bad Edit:

[PROBLEM] My SQL is foobar (returns nothing)

SELECT
    TagName,
    COUNT(*) AS UpVotes 
FROM Tags
    INNER JOIN PostTags ON PostTags.TagId = Tags.id
    INNER JOIN Posts ON Posts.ParentId = PostTags.PostId
    INNER JOIN Votes ON Votes.PostId = Posts.Id and VoteTypeId = 2
WHERE 
    Posts.OwnerUserId = @UserId
GROUP BY TagName 
ORDER BY UpVotes DESC

The problem in the original question was due to TagName being misspelled as TgName; since that was fixed in the edit, the query will work and the problem becomes hidden. Despite improving the code, the title is still undescriptive and hasn't been fixed.

Editing Code in Answers

Answers are intended to be resources for future visitors. While quick and dirty answers are useful, they may have errors in syntax due to being typed in without being checked first, and could have room for improvement. Unlike questions, making an answer work is a good thing and should be encouraged with a few guidelines to follow.

Do:

  • Test your edited code to make sure it works
  • Fix syntax errors and typos

If changing the syntax errors or typos would result in the code doing something other than what the answer says it will, consider creating a comment or a separate answer explaining your change. If posting another answer with only that change seems like it won't provide added value to future visitors, it probably doesn't change logic or functionality and should be an edit instead.

  • Improve formatting
  • Correct spelling/grammar issues in the answer body

Don't:

  • Change code conventions (delimited_names to camelCase names, etc.)
  • Make the code do something different than what the answer says it does
  • Ignore improvements that can be made outside the code block (spelling, grammar, etc.)

Example:

Original Answer:

I think able enough in this code, please check.

$('body').keypress(function(e) 
{
  if (e.keyCode == '13')
  {
     e.stopPropragation();

    }
});​

Good Edit:

This code should work though may need adjustment.

$('body').keypress(function(e)
{
  if (e.keyCode == '13')
  {
    e.stopPropagation();
  }
});​

The code was using stopPropragation() instead of stopPropagation() so that was fixed, the body was edited to be clear to an English speaker, and the indentation was made consistent to improve the code's formatting.

Bad Edit:

I think able enough in this code, please check.

$('body').keypress(function (e) {
    if (e.keyCode === 13) {
        e.stopPropragation();
    }
});​

A few coding and style issues that didn't prevent the code from working were changed, while the show-stopper with stopPropragation() and the nonsensical English was left as-is.

Suggested Edits for Code

If you do not have 2,000 reputation allowing you to edit without review, you should be sure to include a good Edit Summary. Reviewers may not be an expert in the language you are submitting an edit for, and may click 'Reject' if they aren't sure if your edit meets these guidelines. Adding a good Edit Summary will help minimize the chance of them making the wrong decision.

Example:

Good Edit Summary

Corrected typo in a function call (stopPropragation() to stopPropagation()); made indentation consistent

By clearly explaining what the content of the change was and the reason, you are able to direct the reviewer to what they need to look at.

Bad Edit Summary

Fixed Code

If you just say something short like 'edited code' then it is far more likely that the reviewer will have to guess at the reason and intent, and reject the edit if they can't be sure it was appropriate.

13
  • 11
    Keep in mind that changing the indentation in the following languages may also change the functionality of the code and mask the issue just as much as fixing a typo: en.wikipedia.org/wiki/Off-side_rule Commented Feb 22, 2016 at 17:02
  • 1
    When I fix a broken code block, does "Fixed broken code block" suffice? It does sound a lot like "Fixed Code", but I don't think writing "Added indentation so broken code blocks display properly" (or something similar) is that necessary.
    – LarsW
    Commented Apr 6, 2016 at 19:17
  • 2
    There doesn't seem to be a reject reason that goes with this guidance. I'm looking at this edit where the editor changed the syntax.
    – rrauenza
    Commented Jun 22, 2016 at 16:40
  • 3
    @LarsW "Code formatting." should suffice, IMHO, and would cover converting code-as-text to code blocks (inline or actual blocks), indenting (where it does not change the meaning of the code), and un-orphaning first/last lines of code that fell out of a code block. Inspection of the result by a conscientious reviewer will show the details of what you actually changed. Commented Aug 18, 2016 at 20:17
  • 4
    I don't see "Remove unnecessary code" in either Dos or Don'ts. If, for example, a question contains an entire java class (with imports, constructors, getters, etc ), but the description describes a specific method not working correctly, should we remove all the code that is not relevant to the question? Commented Apr 19, 2017 at 7:53
  • 1
    IIRC there have been a few meta topics that directly conflict with this, that say more or less "Don't ever suggest edits to code because reviewers can reject it for any reason because they are not expected to know enough about [tag] to review it", do you want me to go look for them so they can be updated / dupe targeted / voted down / ...deleted? I'm not sure what mechanisms exist for "depreciating" meta posts.
    – jrh
    Commented Oct 11, 2017 at 14:11
  • 1
    In the answer example, I would be tempted to fix the code but leave the body alone simply because the meaning of "I think able enough in this code, please check." is so unclear that I honestly couldn't understand the original intent well enough to offer a better-worded alternative. What should be done then? Edit the code and leave a comment? Leave a comment and not edit? Do nothing and hope someone with better understanding of what the answerer is trying to say comes along to fix it?
    – jmbpiano
    Commented Oct 11, 2017 at 14:54
  • 1
    Should this answer be edited to include the current sentiment of "not changing intent"? This is at least the same in spirit as making it do something different than it says it does, but it is somewhat broader advice. @jmbpiano Downovte for lack of clarity; optionally request clarification in a comment.
    – jpmc26
    Commented Jan 14, 2018 at 4:28
  • Fixing indentation, syntax highlighting, and formatting in C# (not Python or a language where whitespace affects functionality) got me a rather surprising number of reject votes, even with a descriptive edit comment, it would be nice to get some clarification on that.
    – jrh
    Commented Jun 28, 2018 at 15:40
  • 3
    Suggestion: Split the "editing code in answers" to another answer so can be linked to separately - they are separate requirements
    – James
    Commented Jul 22, 2018 at 12:24
  • @James or better yet, stop editing at all because nothing makes sense. Seriously, just improve the content of the site, irrespective of who is doing the edits. Editing to improve the post is always good.
    – Braiam
    Commented Aug 22, 2018 at 10:42
  • @AlexandruSeverin does it makes the post better? If the answer is yes, then yes. Is more sane to ignore this post and just follow the help center.
    – Braiam
    Commented Aug 22, 2018 at 10:43
  • 2
    I strictly simplified a 5 line program (answer) to a 3 line program and had it rejected with a mysterious "This edit was intended to address the author of the post and makes no sense as an edit. It should have been written as a comment or an answer." from 2(!) reviewers. From which I imagine I'm supposed to learn not to bother fixing answers, just post my own. Surely this is wrong?
    – Andrew Kay
    Commented Feb 2, 2021 at 15:51

You must log in to answer this question.

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