6

I've noticed several occasions where a nice clean code snippet in a question is edited by another user to transform the snippet into a working program.

The most recent example I noticed was this question: C++ unexpected implict conversion. Where the original sample code:

struct foo
{
  int operator[](size_t i) const { return 1; }
  operator char() const { return 'a'; }
};

foo f;
f["hello"]; // compilation error desired here

Has been wrapped in headers and a main to transform it into a full program:

#include <cstddef>

struct foo
{
  int operator[](size_t i) const { return 1; }
  operator char() const { return 'a'; }
};

int main()
{
    foo f;
    f["hello"]; // compilation error desired here
}

Personally I find this kind of edit greatly reduces the clarity of the question by adding 'context' which is just not needed. Even the indentation is now inconsistent.

Although this is a minor case, I'm interested in the issue in general. I believe that we would want to encourage people paring down their example code to the minimal example that cleanly demonstrates their problem. These edits seem to be encouraging the opposite.

What is the policy (official or just best-practice) regarding these kind of edits? Am I free to remove the superfluous code and restore my question to its original (and in my opinion, clearer) form?

6
  • 2
    Sounds like the editor transformed it into a MCVE for you... Having an example that will compile is always good.
    – Wooble
    Commented Sep 23, 2014 at 23:56
  • This question seems to be basically "Should questions be edited so the code compiles even if the gist is understandable otherwise?". Could we change the title to that or similar?
    – Anko
    Commented Sep 23, 2014 at 23:58
  • Yes, that's a clearer title. I've changed it.
    – marack
    Commented Sep 24, 2014 at 0:00
  • 2
    You are certainly right complaining about the edit introducing inconsistent indentation. Still, considering how you introduce your snippet, a fully working MCVE is actually expected to follow. Anyway, it greatly reduces the burden on anyone else using/testing your code. Would that snippet have worked, as it was? Sure. Is making it an MCVE an improvement in this specific case? Undecided. Commented Sep 24, 2014 at 0:01
  • 3
    @Wooble This is the exact sentiment that I disagree with. Why sacrifice question comprehensibility for the ability to copy/paste/compile snippets like this? I suspect the vast majority of SO users want to read and understand the code with minimal effort, not compile it.
    – marack
    Commented Sep 24, 2014 at 0:06

1 Answer 1

6

In general, yes, if practical.

The goal for most questions on SO is to include a Minimal, Reproducible Example (MRE). This has many advantages for askers and answerers:

  1. The code compiles. Unless of course the question is about a compilation error. This is useful because anyone interested can download and run the code themselves, which is useful because ...
  2. The code can be verified against the asker's assumptions. Sometimes askers who don't include an MRE will assume that the problem in their code lies in one place, and post just that snippet as a question, when really the problem is in a completely separate area. Potential answerers can download the MRE, run it, and close the question as being inaccurate if the code is misrepresented.
  3. External factors can be reduced or removed. Having random people on the internet able to download and run your code is a great way to find out that your problem only exists when your particular compiler, operating system, etc. is used. This can be difficult to find out without the help of a wider community, and the harder you make it to run your example, the less likely you are to get this kind of help. In fact, requiring anything more than just copying and pasting is going to make it very unlikely that anyone runs your code.
  4. Creating an MRE is a form of debugging. Having to pare the question down to its essentials often reveals where the problem is without even requiring a post on SO. Of course this doesn't apply when editing an example to compile, but it can help get users in the proper habit.
  5. It makes it clear that the user put in effort. For better or for worse, there is a culture of downvoting and closing questions in which the asker didn't appear to try very hard. Going through the effort to create an MRE is a great way to show what you've tried (bonus points if you use the phrase MRE!).

Of course, this isn't practical for all questions. Questions involving the use of multiple libraries or frameworks will often require so much boilerplate for an MRE that it would make the question utterly unreadable. This can also be the case with embedded programming, where nobody has the device you're using anyway, and probably in many other situations.

For your particular situation, was it useful?

I would argue that yes, it's a good idea for your question. It allows people to easily download and run your code. If somebody has encountered your exact situation before, they won't need to do this; but for those of us who haven't, answering your question may require some investigation. I might need to run your code, check the output, form a hypothesis about what's happening, verify my hypothesis ...

Remember, if your question requires some digging in on my part, you're now in a contest between the effort I have to put in, and the interest I have in an answer. Making your question more interesting is hard - you have a fundamental problem you need an answer to, and that's not easy to dress up. In contrast, reducing the effort I have to invest is much easier and worth doing. If you have a question about a particular library function, link to its documentation; if your concern is about some language standard, tell me where I can find the standard text; and if you have a problem with a program, make my life easier and let me quickly run and iterate on that program.

Of course, this is all just my opinion and not an official policy as far as I know, and it might be that the community at large disagrees with this view. If so I suspect we'll quickly find out from the vote counts.

You must log in to answer this question.

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