85

Obviously, a question with an MCVE is almost always best. But we get a lot of questions (at least, my review queue is full of questions) that are basically just two or three screens of unreadable garbage with a "why doesn't this work?".

Between that, and a prose question that the OP has actually thought through, I'd prefer the prose question. If the prose question's comprehensible as it is, is it worth asking for code?

(Thoughts specifically triggered by this question, which didn't originally include any code, just the prose description. As it happens, the added code isn't bad, even if it's not an MCVE, so asking the OP to post code didn't hurt.)

7
  • 2
    possible duplicate of Require or at least remind about code
    – gnat
    Commented Feb 23, 2015 at 20:36
  • 9
    Not seeing how this is a duplicate of a question requesting an automatic prompt. That proposal might benefit from this discussion, but I don't think there's a big overlap given the situation described here.
    – Shog9
    Commented Feb 23, 2015 at 21:02
  • 10
    I'll ask about code if the post shows little effort towards a solution, and might be a "do my homework" question.
    – Sobrique
    Commented Feb 24, 2015 at 10:51
  • 1
    I see an interesting asymmetry here: the screenfuls of code is a reason to downvote ("didn't do your work in trying to solve the problem by narrowing it down"), whereas "not reproducable because no code" is explicitly a reason given for close votes. Commented Feb 24, 2015 at 11:19
  • 2
    @UlrichSchwarz: Both is reason for downvoting, and either might be a reason for closing: Not reproducible, respectively unclear/too broad. Commented Feb 24, 2015 at 14:55
  • 17
    I don't think there's enough information to say for sure, could you maybe post some code? Commented Feb 24, 2015 at 22:44
  • realated: meta.stackoverflow.com/questions/260039/…
    – Tanner
    Commented Oct 2, 2017 at 13:46

6 Answers 6

146

Always? Hell no!

There are problems where having an MCVE is all but essential to finding a solution. If you're at all familiar with the subject being asked about, you can identify these with one eye closed and the other peering through a cloudy malt beverage. If the asker forgot to include the code (or neglected to narrow it down to a specific problem area), then he has made a terrible mistake.

...Then there are problems that don't really need any code, at least not in the question, but where code can be a more effective way to communicate what's being done than prose would be. Doesn't even need to be real code - pseudocode works fine. Your example falls into this category - it didn't need code, but the code is easier to read than the prose, and together they help to clarify the problem in a way that either one alone wouldn't.

...and then there are problems that don't benefit from code at all. The asker is trying to do something specific, but has absolutely no clue how to do it - and any code he might include is just a waste of space:

How do I frob a widget?

I'm building a pugnacious flywheel grommet, and have found that I need to stabilize the system by frobnicating all incoming widgets. Using Java. I've searched the API docs but can find no indication of how this might be accomplished.

Here's my code:

public class Frobnicator
{ 
    public Widget Frob(Widget unfrobbed)
      {
         // ????? 
      } 
  }

These questions are easily recognizable because the code - when included - is either entirely irrelevant to the problem, or entirely boilerplate. Sometimes both. Unlike the second class of problems, the code here actually distracts from the prose - I've seen folks answer these by filling in the blanks without bothering to read the actual problem statement, leading to much frustration on all sides. Fortunately, it's fairly easy to edit out the irrelevance, particularly if you're also providing an answer that makes it clearly unnecessary...

...But then you run into the insidious problem of folks begging for code when none is needed. I've seen folks posting comments asking for code or linking to the MCVE or WHYT articles on questions that were already answered years ago and clearly do not need or benefit from any additions - in at least one instance, I observed an asker browbeaten into copying the solution from an answer into the question, just to quiet the complaints.

That's just annoying.

We already have guidelines for when to include code listed in the How To Ask page. With that in mind, here are some corresponding guidelines for...

When to ask for code

You should politely request that an asker include code when you encounter a question that...

  • ...requires code in order to reproduce the problem.
  • ...would definitely benefit from code in order to better illustrate the requirements described.
  • ...includes too much code to identify the specific problem (be sure to link to MCVE).

You should refrain from requesting code when encountering a question where...

  • ...you haven't actually read the question.
  • ...you have absolutely no idea what is being asked about.
  • ...you aren't sure whether or not code would help to explain the problem.
  • ...the question has already been answered and clearly does not require further code to assist in diagnosing the problem.
  • ...the question does not concern an issue with code.
16
  • 10
    I'll generally ask about 'what have you got' if the question looks like a 'do my homework' question too. More as a way of showing willing.
    – Sobrique
    Commented Feb 24, 2015 at 10:48
  • 1
    @Shog9 - so for example, this question wouldn't require any code? stackoverflow.com/questions/28673567/how-to-draw-a-cutted-cicle. i.e. the question is fine as-is? Commented Feb 24, 2015 at 13:28
  • 4
    @LittleBobbyTables: That is a bad question through and through - no amount of code will redeem it.
    – BoltClock Mod
    Commented Feb 24, 2015 at 14:34
  • @BoltClock - that's what I thought, but apparently five re-openers and six answerers felt otherwise :P *sigh* Commented Feb 24, 2015 at 14:35
  • 15
    @LittleBobbyTables Not all 'do my homework' questions are created equally. In the example you listed, it doesn't matter whether it's a homework question or not -- it's answerable and the question and its answer is useful to others. You may want effort from the OP, but that's a reason to downvote, not close. That question should not be closed if your reason for closing it is that the OP hasn't shown effort.
    – George Stocker Mod
    Commented Feb 24, 2015 at 15:46
  • 3
    Unless there's existing code drawing a circle that he can't modify, adding code isn't going to make that question any easier to answer @LittleBobbyTables. As I see no indication that there's more to the question than what is stated, code would just be noise.
    – Shog9
    Commented Feb 24, 2015 at 16:43
  • 1
    Getting the asker to include the call sites in context of those fill-in-the-blank function can help diagnose XY problems. It can be hard to communicate that you want the call sites, though. Commented Feb 24, 2015 at 19:37
  • 3
    You disagree with not asking for code when you're not sure it'll help, and then go on to describe a scenario where you have a solid reason to believe it will help, @pnuts. That is, you have a solid reason if you're actually looking to narrow down errors or determine the skill of the asker; sprinkling "plz post codez" comments on questions indiscriminately in hope that it'll somehow make them better is still a waste of time.
    – Shog9
    Commented Feb 24, 2015 at 23:55
  • 1
    Semantics are pretty important when it comes to textual communication... Sam states essentially the same thing in a way that might appeal to you more: ask for code if you need code @pnuts.
    – Shog9
    Commented Feb 25, 2015 at 0:05
  • 1
    The main problem is that the close reason “Questioner must show at least a minimal understanding of the problem” has been removed. Since then, a lot of reviewer go round by substituting “must provide mcve” for it, even when they already suspect that showing the code will not reveal more understanding. But sometimes, questioners are really bad at explaining and showing code will really help…
    – Holger
    Commented Feb 25, 2015 at 9:26
  • 1
    I agree with this answer for the most part. Though, I disagree slightly with the notion of not asking for code when you have absolutely no idea what is being asked. I've found sometimes if the OP posts code (even code she or he has searched for) it can move the conversation forward and ultimately lead to the correct solution more quickly.
    – darlirium
    Commented Feb 25, 2015 at 19:17
  • Why beat around the bush though, @williamdnapier? If you've no idea what is being asked, why not just say "I have no idea what is being asked - would you please clarify?"
    – Shog9
    Commented Jul 13, 2015 at 17:56
  • @Shog9 Yeah, asking for code can be beating around the bush. I remember as a beginner, searching for and finding code that might potentially address a problem I was attempting to solve. Sometimes, I could explain what I was trying to do to a more senior developer and sometimes I struggled mightily to do so. I realize if you ask the OP to clarify it should cause them to think through what they're trying to do and that is important. It's just rather than see someone thrash away, if they can supply a bit of code, it might clue me in to what they're attempting to do.
    – darlirium
    Commented Jul 20, 2015 at 14:24
  • 2
    Just spotted a great example of a question where the given code is boilerplate and of no use to readers whatsoever: stackoverflow.com/questions/31667225/… Bonus points for plz-send-teh-codez.
    – BoltClock Mod
    Commented Jul 28, 2015 at 4:17
  • That code is useful, because it shows that they want to frobnicate a Widget rather than an AbstractWidget or some other class.
    – tgdavies
    Commented Aug 7 at 5:57
35

It's a good idea to ask the OP for code, when you actually want to see what the OPs code is.

I know that sounds overly obvious, but it seems to me some commenters haven't entirely thought through what they're going to do with the code once the OP has provided it.

When some people reflexively ask "What have you tried" or "Show us your code", and then turn around and tell the OP to post less code once all the code has been posted, people can get confused.

I personally make it a point to ask the OP for things that I'm actually interested in seeing. I'm interested in the question being easy to read and easy to answer. I'm not so interested in judging the OP.

3
  • 2
    I am often interesting in seeing context, such as structure of inputs. I agree with this anwser. In addition, even if I could write a generic function to answer OP's question, I find that often it is helpful to write an answer using same variable names for instance, because that puts the need to explain what the variables are into the question, splitting the workload better between asker and answerer. Commented Feb 25, 2015 at 9:17
  • 1
    I've often read 'what have you tried?' as 'I think you're being lazy, and asking SO to do it for you. Please prove otherwise. '.
    – Sobrique
    Commented Feb 25, 2015 at 13:39
  • I agree. I interpret, 'What have you tried?' as a way for someone to comment w/o adding any sort of real value to the discussion. The OP should in some cases be willing and able to tell you what she or he has tried. But, more often, they're flailing about and may or may not be able to tell you what they've tried. And, it sometimes shows laziness on the part of the commentator.
    – darlirium
    Commented Feb 25, 2015 at 19:24
13

One of my own most upvoted questions is Yes/No message box using QMessageBox. In it, I offer absolutely no code, I merely ask how it's done. One of the answerers offers a very nice example.

It's not that I couldn't have figured it out by myself in 5 minutes. But as you can see, the question has 30,000 views, which likely means I replaced 5-10 minutes of trial and error with 20-30 seconds of google search. Multiply it by 30,000, and that's a lot of man-hours.

I posted the question more because I thought that the solution to this should be something that comes up in a Google search in a digested form. It's a common problem, and there's absolutely no reason why tens of thousands of programmers should waste 10-20 minutes of their time figuring it out, each one of them.

By the way, at the time I posted it, the Qt docs didn't actually cover that. There was no "Here's how you make a Yes/No message box". You could figure it out after carefully reading them and piecing it all together, but wouldn't it be better to just be able to find it on the Internet? And also the top google hits didn't offer a ready-made solution. There were either no questions, or no code samples offered, just instructions on how to figure it out on your own.

I just wanted to offer that as a counter-point example that sometimes a question would not benefit from adding code to it.

9
  • Well, if it was prominently in the docs or otherwise easily googleable, it would have deserved downvotes. Thus, the side-story was probably critical not only for you posting it, but also for the success. And that makes it a good example. Commented Feb 24, 2015 at 15:00
  • 5
    @Deduplicator Yes. But my point was that there is a general class of problems "How do I do X in language/library Y", where there is no need for posting code, because they're about getting solutions to common problems in that area. These kinds of questions (and there are many others like mine) don't need any code in them. In fact, it would be irrelevant at best and distracting at worst for future visitors coming from a search engine.
    – sashoalm
    Commented Feb 24, 2015 at 15:05
  • 1
    I think the general tenor of the site was different three years ago. If someone were to post that these days, it would look like a no-effort question (and could well be) regardless of whether the docs covered it or not. In that situation there needs to be some negative reaction, otherwise we're back to the problem of encouraging vampires. Now, if the question indicated that the information sought was not in the official docs, perhaps with a link to where it ought to be, then the "prior research" criteria is met.
    – halfer
    Commented Feb 24, 2015 at 22:10
  • You mention that there was nothing from the search engines at the time, but we see all the time new users who say "I googled but didn't find anything", and (of course) often, we find that a trivial search engine search satisfies their requirements. So, perhaps it's a matter of judgement: if I think someone has tried, even if they haven't illustrated it as well as they could have done, I might answer. But, if not, a comment requesting a code attempt is a fair attempt to discourage persons too lazy to use a search engine.
    – halfer
    Commented Feb 24, 2015 at 22:14
  • 3
    @halfer But if the question helps 30,000 future visitors, does it matter to them how much effort OP showed? Is the point of stack overflow to be a user-generated documentation site, the Wikipedia of programming, or to be a help desk? When we post an answer, do we post it only for OP, or for the future visitors as well?
    – sashoalm
    Commented Feb 25, 2015 at 7:18
  • You're right that we post answers for a wide audience. But these days, now the site is much more widely known, we shoot ourselves in the foot if we knowingly tolerate help vampirism. I think the quality of some questions is bad enough as it is (here's my homework, plz do this for me, gimme the codez, etc) without our removing all prior effort requirements entirely. (One possible solution is to folks to downvote and answer anyway, but I won't do that, in case it encourages bad questions on throw-away accounts).
    – halfer
    Commented Feb 25, 2015 at 8:47
  • @halfer Why not close the homework questions but promote the questions that ask how to solve a common problem in language or framework?
    – sashoalm
    Commented Feb 25, 2015 at 8:49
  • If the common problem features zero prior effort, I am minded to continue to close, for the reasons already stated. I think leaving unresearched but novel questions open in case they get a canonical answer is too risky from a quality perspective. However, I do see your point.
    – halfer
    Commented Feb 25, 2015 at 8:59
  • 1
    Related: meta.stackexchange.com/questions/210840/…
    – Shog9
    Commented Feb 26, 2015 at 16:05
11

I'm trying to evolve into responding to questions "in-kind".

If the question contains little or no code, but narratively explains a problem, I (try to) answer by narratively explaining a fix or addition as a proper answer requires.

If the question contains code and narrative, I answer with a mix of code and narrative similar to the question.

I say "try to" because I'm attempting to become a reformed "overly verbose answerer" :-)

My thinking is that the questioner has set the rhetorical standard that they want applied to an answer.

It's kind of like responding to a junior programmer in the hallway. If they show a page of code then I pull out a pen and start marking up the code. If they ask a verbal question (no code), I give them a verbal answer.

2
  • I tend to do the same, though it doesn't always work out. If someone has a problem but doesn't post relevant code, usually I describe the systematic troubleshooting steps to determine the cause of the problem. Then indicate that it will be up to them to use those steps to determine what the problem is, since we obviously cannot in the absence of the relevant code. This IMO is the "teach them to fish" method. Sometimes though they don't even understand their own code to start with and expect alot of hand holding.
    – AaronLS
    Commented Feb 24, 2015 at 23:03
  • 2
    This is also a good approach to "How do I X?" questions. Often I'm interested in the general approach that is common in practice, as reference docs usually don't include common "recipes" and solutions you find online such as codeproject are often flawed. I don't so much care about a detailed implementation, but more of a conceptual description of what approaches people commonly take in practice. I often don't bother though because these questions are ill received these days.
    – AaronLS
    Commented Feb 24, 2015 at 23:08
3

I'd like to see OP's code to ascertain his/her level of knowledge. This allows me to tailor my answer. Especially in homework type of questions it's good way to determine what parts of syntax and/or datatypes have been already covered in their course.

That's why I add sample code even to purely conceptual questions or mine. It tells others where I stand and what I already know.

1
  • The problem with this is askers tend to copy paste code from elsewhere as if they wrote it themselves. Some questions have good code, properly formatted, but OP doesn't even know the basics of the language. They have learnt to pretend well to get free code. On the other hand, codeless questions, where OP knows his stuff, are unreasonably downvoted, closed and even refused to be re-opened, despite requesting in socvr. I think it's a bad idea to judge based on outward appearances.
    – TheMaster
    Commented Oct 22, 2021 at 22:55
-2

I think this is similar, in a way, to the debate about whether code should be commented. There are some who argue that code should be commentless because writing code without comments teaches other programmers to read and understand code quickly, and that it promotes the writing of good unit tests, because programmers should be able to know what code is supposed to do by reading through those anyway.

Real life, however, is never ideal, and the reality is that many (read: most) programmers simply don't have the time to go through hundreds of unit tests every time they need to debug complex code. Moreover, a lot of production code is less than ideal. For this reason, I think code needs to have comments.

Similarly, I think that any good question is usually made up of both code and prose to explain what's supposed to be going on. Even in cases where I don't think prose is necessary, I usually try to include it somewhere just as a good practice. In my professional career, I've found that the best senior-position teachers aren't the ones who've had the longest careers, but the ones who are able to communicate in English like human beings at least as well as, if not better than, code.

You must log in to answer this question.

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