76

I understand that it is necessary to create a Minimal, Complete, and Verifiable example when posting questions to StackOverflow.

However, when dealing with UI related questions such as WinForms in Visual C#, a lot of the code for creating the Form tends to be very long, and are auto generated by Microsoft Visual Studio's designer.

For example, to create a simple form with a ListBox and a Button

enter image description here

The following code is generated:

http://pastebin.com/unAJLk1S

http://pastebin.com/2nTEnXtL

http://pastebin.com/tak2fpBA

Meanwhile, the only code that I wrote is only a few lines in Form1.cs: http://pastebin.com/BNMCnKzX

So my thought was that for a Minimal, Complete, and Verifiable example, I should only include the code that I actually wrote, and not the code that was automatically generated. However, based by the close vote on my question Why does the SelectedIndexChanged event fire in a ListBox when the selected item is modified?, this does not seem to be the case.

So my question is, how much code do we need to include for a Minimal, Complete, and Verifiable example when posting UI related questions in C#, or in general?

12
  • 11
    If WinForms is anything like Java's Swing toolkit, you can usually manually write much shorter code than a WYSIWYG GUI designer will generate. (As a bonus, if the handwritten code works, now you have a debugging lead.) Jan 3, 2017 at 23:49
  • Side note on your linked post: in addition to MCVE it is good idea to do your research and look at the source code - referencesource.microsoft.com/#System.Windows.Forms/winforms/… (no idea if there is an answer, but it would not hurt to know exactly how it is done) Jan 4, 2017 at 2:30
  • 7
    @JeffreyBosboom No, WinForms and Swing are completely different. Try to change the WinForms auto-generated code and you will see all the problems you get. That's what WinForms is for. BTW, debugging the auto-generated WinForms code is very easy Jan 4, 2017 at 11:56
  • @CamiloTerevinto Winforms is a nightmare move to WPF and give yourself more control.
    – user692942
    Jan 4, 2017 at 12:32
  • @Lankymart moved to WPF long time ago but mostly only use MVC anyway Jan 4, 2017 at 12:35
  • Why it is in meta.stackoverflow.com?
    – Artur A
    Jan 5, 2017 at 8:31
  • 7
    @Artru Where else would this question be asked? It is a question about the process of asking questions on Stack Overflow, which is what Meta is for. It would be off-topic on Stack Overflow. Jan 5, 2017 at 8:40
  • 1
    @CodyGray I see now the point, thanks
    – Artur A
    Jan 5, 2017 at 8:45
  • 1
    I have a query regarding this. If I am a C# developer with some experience, I know what parts of a code are generated and what parts are relevant to the question. If I am a newbie, I do not. What could newbies do in such a case?
    – Ranveer
    Jan 5, 2017 at 9:59
  • @Ranveer I'm confused - I don't see how you could possibly not know which bits of code were human-authored if you've just created an MCVE.
    – Mark Amery
    Jan 5, 2017 at 16:59
  • @JeffreyBosboom If the handwritten code works, doesn't that just make your job harder, since you are now without a MCVE to post? Jan 5, 2017 at 20:22
  • @KyleStrand If you want to ask a question, sure. If you're willing to solve the problem on your own, you can now look at what the autogenerated code does that the handwritten code doesn't, perhaps gradually building up the handwritten code until it stops working, or stepping through both in the debugger and inspecting the state of the widgets to see if/how they differ. That's a better place to be than "this huge block of autogenerated code doesn't work and I don't know why". Jan 5, 2017 at 20:29

4 Answers 4

74

Contrary to the advice of people who try and follow black-and-white rules instead of using their brain, you do not need to create a "complete" code-based example for questions about standard UI controls. If you've placed a ListView control on a form, adjusted a few of its properties, and wired up some event handlers, then you do not need to (and, in fact, should not) reproduce all of this as code. It is sufficient to explain what you've done in words so that someone competent in the technology can follow your explanation to reproduce the equivalent setup on their screen. In the above example, then, it would be sufficient to indicate that you've added ListView control, provide a list of the properties you adjusted and to which values, and then show the code you've added to the relevant event handlers.

The standard "MCVE" advice, while not wrong, is primarily for beginners who have no idea how to go about asking a good question. If you know the tools and can confidently answer the question "Can someone with reasonable knowledge about this technology use the information I've provided here to reproduce my problem?" in the affirmative, then you are good to go.

If you are not sure whether or not you have provided enough code or description, then it is easy to verify. Open up a blank project and try to reproduce the problem using only the information provided in your question. Could you do it? If not, go back and figure out what critical information was omitted the first time.

Minimal is good. No one wants to look at a question with huge blocks of boilerplate code that accomplishes trivial tasks. If you can't figure out how to drop a control onto a form and wire up an event handler from only a brief textual description, you have no business answering a question about it.

I have no idea why someone cast a close vote on your question. There is nothing wrong with the description and code provided in your question. I'd even go so far as to say that there's no reason to explain how you populated the control with arbitrary values for testing purposes. That isn't an interesting aspect of your question. Fortunately, the code is short enough that including it as you did doesn't hurt, either.

11
  • 4
    Auto generated code and libraries should not be treated differently, both are a bunch of code that makes programming easier. If we ask to reduce generated code, wouldn't we also ask askers ro reduce libraries?
    – Braiam
    Jan 4, 2017 at 12:15
  • 15
    Yes, we should ask people to reduce libraries. Don't reproduce the entire Boost header in your question. Just tell people that you've included that header. They're responsible for downloading the library and configuring their environment to use it if they want to test the code. Each question should not be a self-contained "How to Program" tutorial. Jan 4, 2017 at 12:22
  • 2
    @Gimby The OP edited the links to the code into the question in response to the close vote, not the other way around.
    – Servy
    Jan 4, 2017 at 14:37
  • 1
    The problem is not that an expert in a subject isn't capable of adding a listbox to a form in given that description, the problem is that a textual description of such instructions is highly likely to be imprecise and for there to be many different interpretations of it which may or may not actually result in a reproduction of the problem, particularly when you get past the most trivial of cases.
    – Servy
    Jan 4, 2017 at 14:48
  • 18
    This is a trivial case, and most of them are. In the non-trivial cases, more effort is obviously going to be required. That goes to the point about the MCVE advice being primarily for beginners, who can't be expected to distinguish between trivial and non-trivial cases. Mature programmers can determine for themselves whether their descriptions are sufficient. Let's not overstate the reality here. There aren't multiple valid interpretations of adding a control to a form. It is sad that this answer is controversial. Jan 4, 2017 at 15:07
  • 'Beginners' in this context being people new to programming as a whole then, you can also be a beginner when it comes to a particular language/tool/platform/etc. but an experienced engineer can ask reasoned questions about it and thus deserve more leniency when it comes to the MCVE requirements.
    – Gimby
    Jan 4, 2017 at 16:39
  • 7
    +1 for this answer. I've had exactly the same issue as the OP. Instead of posting wads of irrelevant code, I gave clear enough instructions that anyone familiar with the language and IDe would easily be able to reproduce them. I ended up having quite an argument with someone in the comments as to how exactly I was supposed to provide a MCVE without posting wads of code. In the end, I just gave up and abandoned the question. Whilst this site is great, it has a big disadvantage in that it's too easy to downvote anonymously, with no comeback or explanation needed. Jan 4, 2017 at 18:11
  • 1
    @CodyGray In the "trivial" case of just adding one control to one form it's just one line of code. You don't need to avoid posting it. Why work so hard to avoid actually providing a reproducible example when it's so trivially easy to do, given that any situation where it would be difficult to actually provide an actual reproducible example would also be extremely difficult to precisely describe the steps to reproduce without code?
    – Servy
    Jan 4, 2017 at 18:47
  • 7
    The more lines of code that appear in your question, the harder it is to focus on the relevant pieces of code and thereby identify the problem. If you clutter it up with a bunch of trivial declarations and initializations, you're just wasting my time. It's like having a bunch of #includes/usings, a main function, etc., all in a vain attempt to make it so that I can copy the code into my IDE and hit run. There's a <5% chance I'm going to do that, and I'd rather not have to do that. The way you facilitate me not having to do that is omitting noise and focusing on the relevant pieces. Jan 4, 2017 at 18:49
  • @Avrohom Yisroel: Your last sentence doesn't quite follow... you said you had an argument with someone, so they did explain their downvote, didn't they? Otherwise you wouldn't have had that argument.
    – BoltClock
    Jan 7, 2017 at 2:57
  • @BoltClock Sorry, wasn't quite clear. The argument was about whether or not I had provided a MCVE. I have no idea who gave the downvote, and my comment about it was really a separate complaint that it's too easy to downvote without any comeback. Should have split the two comments! Jan 7, 2017 at 17:48
41

Go through each property one at a time:

Minimal

If it doesn't need to be there, remove it. You shouldn't be including pages and pages of code that have nothing to do with reproducing your problem.

Complete

It needs to be a complete program; it cannot require code not shown to function as described.

Verifiable

That complete program needs to replicate the problematic behavior you've described.


If you're omitting code necessary to reproduce the problem, then you're not providing a complete verifiable example. If you're just dumping a giant auto-generated file with tons of code that has nothing to do with the problem, then you're not providing a Minimal example.

If you have a question that requires a form with a listbox in order to replicate the problem, then create a form with a listbox to replicate the problem:

var form = new Form();
var listbox = new ListBox();
form.Controls.Add(listbox);
//TODO other code needed to populate the listbox.

Of course, you need to make sure that all the designer code being cut out isn't necessary to reproduce the problem, if some of it is, figure out the smallest program you can create that still reproduces the problem, and provide that.

Whether the code in your application was written by you or not has nothing to do with whether it's a part of a minimal complete verifiable example. You don't automatically exclude auto-generated code, nor do you automatically include manually written code. You include what you need, and only what you need, to replicate the problem.

38
  • 25
    +1 for the essence: If you're just dumping a giant auto-generated file with tons of code that has nothing to do with the problem, then you're not providing a Minimal example.. This applies to a lot of Android questions too. Jan 4, 2017 at 0:51
  • 49
    I don't wholly agree with this. Trimming down autogenerated code isn't always a good idea; it may not be possible at all, it may take hours, it may be outside of the ability of a beginner to a framework, and it may result in code that has little resemblance to the normal architecture of an application in the framework. A perfectly acceptable alternative exists, which is to provide precise instructions on how to generate the code using whatever the code generation tools the framework provides (e.g. run new-proj-generator then paste this code into the body of init_widgets(): /* code*/.
    – Mark Amery
    Jan 4, 2017 at 11:46
  • 3
    @MarkAmery that's likely because you are used to working with bloated unnecessary auto generated fluff. Rule all programmers should live by if you don't understand it, it shouldn't be there. It's the difference between a 10 line and 100 line programs. If removing auto generated code is causing you problems then you're doing it wrong imho.
    – user692942
    Jan 4, 2017 at 12:28
  • 19
    @Lankymart if you think there are any "100 line programs" to be trimmed down in the sort of contexts I'm talking about, then you're missing my point. Even the OP's example (in a framework I'm unfamiliar with) has hundreds of lines of generated code, and that's a modest example. Many frameworks have project creation scripts that start you off with hundreds of files of code; some graphical UIs create code that isn't even meant to be read by humans. "Trimming" down such projects to let you paste all the code needed for a runnable example is not viable and would be pointless even if it were.
    – Mark Amery
    Jan 4, 2017 at 12:49
  • 5
    @MarkAmery If the instructions is "create a project and then copy this code into this file" then sure, that's easy reproducible instructions to provide, when the instructions involve actually using the WISYWIG editor it very quickly becomes extremely difficult to actually provide precise instructions that will unambiguously allow everyone to reliably reproduce the problems, unlike an actual code example. The point is not to manually re-create the entire program without using the code generation tools, the point is to replicate the problem, which is very different.
    – Servy
    Jan 4, 2017 at 14:34
  • 3
    Why, if we're looking at a file that is required to reproduce the problem but doesn't contain my code that has the error, should it be minimal? If the autogenerated file starts at 1000 lines, and I spend an hour or two trimming it down to 100 lines, how does that help? The bug isn't there, so people don't need to look through it. And they're not typing it in...they're going to cut and paste it either way. The code file(s) I'm working on that has the error should be minimal, of course, but supporting files...who cares?
    – Beska
    Jan 4, 2017 at 18:52
  • 3
    @Beska So you just go around blindly copying thousand line of code files without reading them and running them on your computer blindly without any consideration? And anyway, if you're that confident that there's no problem there, and that the problem is in some other specific method, it should be very easy to construct a simple test that demonstrate just that one method. As I've said before, you don't need to re-create the entire application, just the one problem. If it's taking you hours and hours to construct a minimal example, then you probably don't know where the actual problem lies.
    – Servy
    Jan 4, 2017 at 19:01
  • 2
    @Servy It seems to me like there are many cases where a beginner is going to start trying to do something using a framework where a ton of files are created for them, and everything works fine. They add a couple lines of code to the one code file they're trying to use, and things don't work. They can trivially create a minimal example of their own code (and it may be minimized already)...but all of those extra auto-generated code files? They can be pretty sure they know where the problem is (in the one file they've edited), but still have no simple way to minimize all the other files.
    – Beska
    Jan 4, 2017 at 19:06
  • 2
    @Beska Sure, if you're just using a framework you can simply say that you're using a framework, but if you're using a WYSIWYG editor to create a bunch of code, you can't just say, "this is the only code that I actually wrote" because that doesn't allow anyone to reproduce the problem.
    – Servy
    Jan 4, 2017 at 19:09
  • 1
    @Servy Definitely agree with your WYSIWYG example! I'm definitely not against the concept in general. A small, minimal example is the ideal to shoot for, of course. I find that when I'm generating one, I often find my own solution to the problem, as extra cruft (such as WYSIWYG garbage) is carved away.
    – Beska
    Jan 4, 2017 at 19:09
  • 1
    This is good advice in general, but what about edge cases where the smallest complete example is too large to consider minimal, but a truly minimal example cannot be considered complete? (For example, when someone is asking a question involving auto-generated code, but don't know enough about the tool they're using to be able to hand-optimise its generated code into a more compact form without breaking things worse than they already are.) In this case, would it be reasonable for them to post their own code directly, say that the rest was auto-generated, and upload it to pastebin or github? Jan 5, 2017 at 18:56
  • 1
    If they're using something that requires a ton of boilerplate to work properly, or a WYSIWYG editor that doesn't care about readability, there could easily be a ton of relevant, but not directly problematic, code. Putting it in the question would pollute it (and very possibly push it over the character limit), while having it offsite would prevent the question from being self-contained (and can make the question unusable if their site of choice ever goes down). I can't see any "right" solution for this particular kind of edge case, in all honesty; everything I think of is flawed. Jan 5, 2017 at 18:58
  • 1
    @JustinTime At some point they still need to create an appropriate example. If it's too hard for them to actually figure out where the problem is in their application beyond, "something in the entire application isn't working" then we can't really help them. There's already lots of advice out there on how to create an MCVE that I can't just repeat here, but in such a case starting from a blank application and adding in what you need to replicate the problem would likely be easier than trying to remove from a large application until all you had left was a minimal example.
    – Servy
    Jan 5, 2017 at 19:05
  • 1
    @Servy That's not what I mean. I'm explicitly talking about certain edge cases where "minimal" and "complete" clash with each other, as I said. Auto-generated code, or a particular tool's boilerplate, would be required for it to be "complete". However, if this code is included, it cannot be considered "minimal". Most users that would need to ask a question like this won't be able to hand-optimise the auto-generated code into a more compact form without breaking it, therefore they would not be able to shrink it. Jan 5, 2017 at 19:12
  • 1
    Even if they know that the problem is specifically in their code, they would have no idea how much of the auto-generated code they can safely remove while leaving their question complete. Therefore, that leaves them with the choice of either ignoring the "minimal" part once they've done everything they can to minimise their own code, or providing the auto-generated code on a third-party site instead of directly in the question. Jan 5, 2017 at 19:13
12

This actually goes to the Verifiable part of the acronym:
A reader should be able to reproduce the problem.

So it could be as Minimal as

Drop a Button and a ListBox on a Form and add the following code to the Button_Click handler:

 ListBox1.Items.Add("Hello");
 ...

That is Complete enough.
Handwriting Forms code is not for beginners and may mask part of the problem.

4

Most of the UI questions I've ever seen asked, don't really need a reproduction - an image of the ui and a description of the issue is normally enough to understand the issue. You can always add the pastebin for those that wish to delve deeper, but your question shouldn't rely on it.

Here's an example question of mine that hopefully demonstrates a UI issue without the need to see all the code.

2
  • 1
    "enough for most that are able to help" - that's an extremely dangerous description. It's used as an excuse by people who don't care about posting an mcve when one is actually required. More often than not, it appears that they indeed were omitting important details, but those asking for code are shunned with "if you know what I mean, you know how to reproduce it".
    – CodeCaster
    Jan 5, 2017 at 10:33
  • 1
    @CodeCaster - You're right, I perhaps worded that incorrectly. I was trying to describe those that are familiar with the ui framework enough to visualize the issue and would know what the code would look like
    – Sayse
    Jan 5, 2017 at 13:33

You must log in to answer this question.

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