12
\$\begingroup\$

I've made a couple of suggested edits to this question. The first one was rejected because it deviated from he original intent / it edited code. When I looked at the suggested edit, it did look like I had just hacked away at the code, so I wasn't surprised it had been rejected.

I made a second attempt which was approved, however the edit to the code block was undone by @mdfst13 as part of the approval. The edit I've been attempting to make is essentially to remove 8 spaces from the entire code block. Currently it's indented like this:

        code starts here

And I've been trying to adjust it so that the code starts at the left of the code block:

code should start here

I'm not changing the content of the code, or the relative indenting of the code within the block, simply adjusting the whole block to the left. This answer suggests that this kind of edit might be ok, however it's a couple of years old and site attitudes change over time. Part of why I'm suggesting edits now is because I want to get a feel for what is/isn't acceptable to the community before I get editing privileges, so what is the guideline here, is it acceptable to remove white space from code blocks if it doesn't change the shape of the code, or is the basic rule, Don't change code blocks in questions at all?

\$\endgroup\$
3
  • 9
    \$\begingroup\$ I personally think the edit you did looks just wonderful, and I'm seriously considering rolling back the rollback \$\endgroup\$
    – Vogel612
    Jun 15, 2016 at 21:39
  • \$\begingroup\$ Only the code block bit was undone, the rest of my edit was left intact, so it seems to be the code sections that people don't like being changed. \$\endgroup\$
    – forsvarir
    Jun 15, 2016 at 21:45
  • 1
    \$\begingroup\$ Related (Duplicate?) Edit dispute - Indentation on questions \$\endgroup\$
    – rolfl
    Jun 22, 2016 at 10:19

3 Answers 3

15
\$\begingroup\$

In general you shouldn't be editing code in the question, but instead reviewing it. But there are at least two cases where it's fine to make an exception:

  1. Where the poster has fumbled the formatting. See for example this question, where the poster didn't know how to follow a numbered list with a code block. It's fine to fix this kind of mistake.

  2. Where excessive common indentation causes the code to run off the right-hand side of the window, making it unreadable. It's fine to remove the common indentation in the name of readability.

Your edit falls under exception (2) and looks fine to me. Accordingly, I've rolled back to your version of the question.

\$\endgroup\$
4
\$\begingroup\$

Indentation vs conventions

I recently declined the following suggested edit: removing-leading-white-space-in-question-code-blocks.

While the indentation was a good suggestion, the editor took it a step further and also changed whitespaces in the code. This to me indicates deviating from the OP's intend (not indent, or maybe also indent .. mhmm). By doing so, you are also taking away the opportunity for reviewers to review the coding style conventions and used guidelines.

enter image description here

\$\endgroup\$
2
\$\begingroup\$

The general rule is that people should make code look like it does in their IDE. It is common in an IDE for code to appear something like

namespace FOO {
    class BAR {
        BAR() {
            // elided for brevity -- please don't do this in real questions
        }

But then people remove the context:

        BAR() {
            // elided for brevity -- please don't do this in real questions
        }

I don't consider this to be overly indented, as it matches what they have in their IDE. If they later restore the context, they shouldn't have to reindent the code block.

When I do edit code blocks

BAR() {
// elided for brevity -- please don't do this in real questions

}

This block is obviously wrong. Some of the code isn't even in the block.

BAR() {
    // elided for brevity -- please don't do this in real questions
}

This seems more likely to be what they intended (when you use the GUI to start a code block, it indents the first line by four spaces).

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for explaining your thought process, it has made me think more about the edits I make. I was hoping for a resounding vote one way or the other, but since it's pretty even, I've accepted the answer that's closest to my thinking. I think where the whole block is moved to the left I still see value in the edit, because I don't think this 'If they later restore the context, they shouldn't have to reindent the code block' will often happen. They aren't supposed to edit code after answers, so can't add context and any answers they take into their IDE may have different indenting anyway. \$\endgroup\$
    – forsvarir
    Jun 21, 2016 at 21:41

You must log in to answer this question.

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