13

Problem: Many users post code for a MCVE that does too much.

Let's say, for example, a program reads from a file, replaces some characters with other characters, replaces some other characters, conditionally removes some lines and writes to a new file. And this program doesn't work.

Of course posting such a program would not make for a particularly good Stack Overflow question, as this program does too many things to have much future value.

Now some might say a program that does the above is a minimal example, since one may not be able to shorten the program much further while still doing all of the above things.

Solution: Explain MCVEs "do exactly one thing" (i.e. it's Minimal)

The MCVE help center page doesn't currently explain what exactly we mean by "minimal" - it's kind of left up to any given person to interpret as they wish.

As such, I propose we include a part to clarify this. For example:

Minimal

Your code should ideally do exactly one thing, with no unnecessary code.

If you have a program with many lines of code which does a few things (e.g. reading from file or command line, replacing some characters, removing some lines and writing to file), this will make it hard for people to find your problem.

You will receive better answers if you create a new program that focuses on only the one thing you're having a problem with (e.g. replace a character in a hard-coded string and just display the result on the command line).

Streamline your example in one of two ways:

...


Addendum: Improve the "Complete" part

As an addendum, I might also suggest an improvement to the "complete" part:

Complete

Post all your code and all files your code needs to run.

Include any input your code requires, whether it's input from the user or from a file (although it's best to change your program so it can run without any input).

  • Some people might try to test your code and possible solutions to help them give you an answer that works.
  • The problem might not be in the part you suspect it is, but another part entirely.

If a web page problem requires HTML, some JavaScript and a stylesheet, include all three.

18
  • 2
    I'm not sure I agree with this. (In PHP - tag I follow) We often have to ask people to post more, as the provided code is not enough, such as a form or an included file in which the content is the key to solve it. The same goes to your example: What if we assume the file is being loaded in the first place, but the provided code is only showing the part where OP's trying to replace the content? And then, it turns out OP only has a variable containing the file name, not it's data..
    – FirstOne
    Jan 14, 2018 at 11:42
  • 18
    It seems contradictory: "Post all your code" and "do exactly one thing" don't look well together..
    – FirstOne
    Jan 14, 2018 at 11:44
  • 1
    In the OpenGL tag I mostly follow the same thing happens. Screen is black -> op shows drawing code and shaders -> problem is the shader loading which is not shown. I prefer it way more if I see everything the user does and which is related to the problem.
    – BDL
    Jan 14, 2018 at 11:45
  • 1
    Side note: I don't think your current example is too big or doing too many things - it's a simple enough scope.
    – FirstOne
    Jan 14, 2018 at 11:51
  • 6
    @FirstOne You should first construct a minimal example, and then you should post all the code of that example. You shouldn't post all the code of your original program. I open to the possibility that my suggested changes (or the original phrasing) might not make that particularly clear, but I'm not sure how to fix it in an elegant way (if it needs fixing). Jan 14, 2018 at 11:53
  • 27
    It's clear, imho: "Restart from scratch. Create a new program, adding in only what is needed to see the problem." I think the problem is that the people you're trying to reach don't even read such recommendations in the first place - and that's the real issue.
    – FirstOne
    Jan 14, 2018 at 11:57
  • 2
    @FirstOne I read "Restart from scratch" more as a suggestion of how to end up with a minimal program as opposed to explaining what a minimal program is (someone might say "my program is already minimal, I don't need to start from scratch"). But I agree that most users probably never find their way there, I'd just like the advice there to be a bit more explicit so anyone who does find their way there (especially those directed there through comments or the close reason) knows exactly what they should be doing. Jan 14, 2018 at 12:02
  • I'd like to add to Complete:: if your code requires certain data from a database, include a sample table, including data types.
    – Erik A
    Jan 14, 2018 at 13:12
  • 23
    If OP's put in that much effort, they might, [gasp!], start debugging stuff for themselves. Is it socially, morally and professionally responsible to start such a new and revolutionary trend? Jan 14, 2018 at 13:13
  • 4
    Gonna be honest; I take umbrage to your blanket assertion that a question like that will hold no future value for anyone.
    – Makoto
    Jan 14, 2018 at 19:03
  • @Makoto I didn't say there'd be no future value for anyone, just that there won't be much future value, and, by implication, that there will be more future value if the question were just to focus on one thing. Do you take issue with my example (which I'll admit might be flawed, because something like "replace characters in a file" could be minimal in some cases) or do you, in general, believe that questions don't need to be minimal (or do you have another definition of "minimal")? Jan 14, 2018 at 19:35
  • 4
    In all frankness, I take issue with the whole MCVE moniker nowadays. It feels like it's being used to shut down questions unnecessarily. It doesn't feel like it communicates what we're looking for (details in their question). Your example is also a bit flawed in that this is a textbook case of an OP posting a question with their code, yet being told that it's "too much" and it needs to be pared down without really describing what is overabundant. I do feel like questions should be as simple as possible, but the moniker is, frankly, annoying.
    – Makoto
    Jan 14, 2018 at 19:38
  • 2
    @Makoto "without really describing what is overabundant" - that's why I posted this feature request... Jan 14, 2018 at 20:29
  • 11
    Cue endless debate on what constitutes "exactly one thing" - it's not like that phrasing is any better than "minimal".
    – BoltClock
    Jan 16, 2018 at 8:11
  • 1
    A question which asks "I have located the problem to the replace characters part, but I don't know which of the replace character algorithms that is the culprit" (code of those functions only follows), is a whole lot better than "here is my whole bloody program, it is not working". Neither is a MCVE but the former might be narrow enough.
    – Lundin
    Jan 16, 2018 at 15:19

2 Answers 2

53

Your heart's in the right place, but I think the suggestion misses the mark.

Just as "minimal" is often misinterpreted, so too can "do exactly one thing". People who are inclined to just dump code into their question in spite of the "minimal" guidance are just as likely to rationalize that their code does just "do exactly one thing", where that "one thing" is along the lines of "allow the user to have access to a full-featured spreadsheet".

There is also the risk of confusing question authors, who may look at their code and see "well, I initialize this string and I try to execute a regex pattern match on it…isn't that two things?"

I think stating that the code should "do exactly one thing" just provides yet another way for question authors and the people who would try to help them to disagree on what constitutes a MCVE. The scope of "one thing" is still open to interpretation, and is in my opinion no more clear than "minimal". In fact, if anything, it seems less clear, because it has the trappings of being much more prescriptive, but requires the question author to figure out how broadly to interpret "one" in the context of their question.

Writing a good code example is an art, and takes practice. Unfortunately, those who have the greatest need to provide a good code example are the ones with the least amount of experience providing them. That said, Stack Overflow has plenty of good guidance to make it completely clear what a good code example looks like, for those who will take the time to read it.

We not only have the main MCVE article, we also have the article answering the question "How do I ask a good question?", and that article has links at the bottom to several excellent essays that elaborate in great detail on what makes a good code example.

For the person who is inclined to perform due diligence, including properly researching their question before posting it, and reviewing all of the available guidance here on how to post a good question, the available information is more than adequate. For the other person, adding more detail to the guidance in the form of an instruction that their code example should "do exactly one thing" is at best just a waste of time, and at worst is going to confuse them even more than they already are.

3
  • 3
    Defeatist attitude with no explicit prove to back it up should be avoided. Everything can be misinterpreted, it doesn't mean that one explanation can't be more effective than another. There are no fool proof ways to communicate technical concepts. It doesn't mean that we should close Stack Overflow and go home. Answers can be misinterpreted, it doesn't mean all answers are equally effective at explaining the corresponding solutions. "do exactly one thing" is concise and pushes in the right direction. If many people will find it confusing (I doubt it), we can revisit it.
    – jfs
    Jan 15, 2018 at 18:55
  • 19
    @jfs: "Defeatist attitude with no explicit prove to back it up should be avoided" -- and where is your evidence that the proposal actually "pushes in the right direction"? That's just as speculative a statement as anything else here. And frankly, Stack Overflow is filled with the evidence of the futility of improving the rate at which question authors provide good MCVEs. There's a lot more evidence supporting my speculation than yours. Jan 15, 2018 at 21:55
  • It might not be perfect, but I do think it's an improvement on the current phrasing, which says little more than "less code is better". It even includes a short example to clarify what exactly this "thing" we're talking about is. Jan 16, 2018 at 15:47
30

I think that at one point or another you'll have to accept that people who ask questions on Stack Overflow are generally not capable of creating an MCVE.

For example, in the C# and LINQ tags, a lot of questions are asked about processing a collection of data. These generally come from a database or text file. But the database and text file are not relevant to the problem. Nor is the fact that the code runs in an event handler, or in a Web API Action Method, or in a Windows Service.

All they have to do is create a new console application in Visual Studio, instantiate an array of a handful of object that represent their data set, and write some LINQ statements*.

But they don't. They don't know their problem pertains to LINQ, they don't know how to create a small program that reproduces their problem, because if they could, chances are that the creation of that small program alone teaches them enough to solve their own problem.

So this is actually a hard problem. You can't say "post code that does one thing", because that'll trigger them to just post their LINQ statements, without any data.

The solution? Users should vote to close as non-MCVE way more often. The sacred texts describing how to ask an answerable question have been refined ad nauseum, to minimal results. People will just need to learn to read and apply them. The last time I checked, Stack Overflow still wasn't a free debugging service.

*: And yes, of course LINQ to Entities can behave different from LINQ to Objects, but that doesn't apply to a lot of questions.

8
  • 3
    I agree that it would probably be better if we can get everyone to agree to not answer and instead vote/flag-to-close, but that seems much, much harder to achieve than just making some improvements to the help pages. Although "one thing" might not be the best suggestion, and the initial "How to Ask" page is probably a better candidate for improvement. Jan 15, 2018 at 10:21
  • I think you made a great example here. I think the How to ask page is missing some broader examples like this. Jan 16, 2018 at 4:24
  • 1
    Interesting take on it. If you put it like that yeah, I can see how the MCVE closure reason is primarily there to keep these kind of questions from ending up in limbo rather than it being a tool to get people to write better questions. Because as you say... most people simply aren't ready yet to be able to walk the extra mile, they don't need an answer but personal assistance and tutoring. Huh. Never thought about it that way.
    – Gimby
    Jan 16, 2018 at 12:10
  • 1
    "Users should vote to close as non-MCVE way more often" maybe users with gold badges should be able to close questions more easily.
    – Braiam
    Jan 16, 2018 at 16:40
  • @Braiam: what form would "more easily" take? As a gold-badge user, I'll state my opposition to a solution that involves single-handedly closing, a la duplicates. I admit I'd love to be able to do so, but close reasons other than duplicate are a lot more subjective and I think it makes more sense to make sure other community members concur. The most I'd do is weight a gold-badge holder an extra vote; i.e. other close reasons could be applied with only four, or even three users, if enough of those users had gold badges in the relevant tag. Jan 16, 2018 at 17:49
  • Similarly questions in unity3d have a problem: many questions revolve around an issue of null pointers (which all get closed as exact duplicates) but they all share one thing in common: trying to get an object by name from the active scene. Without the scene data, the question cannot be "complete" but experienced developers with Unity don't need this data most of the time. Occasionally yes, we have to ask for more info, but what form that info takes varies. There's no "include this" we could ask for up front. Jan 16, 2018 at 18:52
  • 1
    @PeterDuniho "a lot more subjective"? Are you saying that determining a "off topic" question is subjective? How? I, for one, would without any trouble close anything single-handely if I believe that it should be closed, as it's the right thing to do and the same thing I would do if I didn't had the power to do so.
    – Braiam
    Jan 16, 2018 at 19:52
  • @Braiam: for some of the reasons, yes they are subjective. For example, "unclear". I have seen questions where I felt the author failed to express a comprehensible question, but someone else has been able to decipher what they meant and answered it. Likewise, questions lacking a MCVE, occasionally still get answered usefully even though I wasn't able to see the problem without the MCVE. I could go on, but comment character limits. More to the point, closing as duplicate typically helps the question author directly, while other closure reasons not so much. Jan 16, 2018 at 19:56

You must log in to answer this question.

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