Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 158 Stack Exchange communities.

What is meta?
Here's how it works:
  1. Any Stack Exchange user can ask a question
  2. The community provides support, votes on ideas, and reports bugs
  3. Your voice helps shape the way Stack Exchange operates

Very frequently I step into questions on stackoverflow that have small snippets of code, each with it's own set of custom types/pointers/maps/vectors/classes (and other stuffs). Sometimes there's simply not enough code to reproduce the error they claim it's happening, or the code itself may not be very clear.

I try to give the best answer I can and sometimes that involves having to rewrite part of the code that was hidden by the OP. To make sure the OP understands every step of the way and that there is no bug in the parts left behind, I might code a complete application, mostly because some of these guys seems completely noobs.

Is sharing the full source code on an answer considered to be a bad/good practice? Is it harmful? Is there a standard for this situation?

share|improve this question
It seems your question is "how much code do I put in the answer", but the accepted answer addresses "how much code do I put in the question" - as do the other answers. Am I misinterpreting - I actually came here looking for the answer to your question, not the answers that were given...??? – Floris Apr 22 '13 at 0:07
Turns out, you are right. Feel free to answer the question. – karlphillip Apr 23 '13 at 1:41

You should post what code is needed, but it is hard to gauge that sometimes. There usually isn't enough room in the box for too much source code, and new users that you mention may get lost with too much code.

A hard line to walk.

share|improve this answer
I don't have the rep to upvote this (meta apparently doesn't share rep with SO) but I agree. I add this comment to advocate using third-party online code hosts, so that people who view the question can view the full source if necessary. This solves both clutter and thoroughness. – lunchmeat317 Apr 2 '11 at 0:56
@lunchmeat317: I'd upvote this for you, but hey I wanted to upvote it myself. ;) – JoseK Apr 2 '11 at 4:52
@lunchmeat: 3rd-party hosts suck because they can get bought by Google and shut down, change their URLs, or just be unavailable (down, blocked by firewall, etc.). – Gabe Apr 2 '11 at 5:01

Its very difficult to work out how much code to post - too much and your post becomes a wall of code that makes peoples eyes glaze over, too little and nobody can actually help you.

I tend to err on the side of including too little code and don't include code unless it seems to be directly linked to the problem.

Remember - people can always ask you to elaborate on your post if you have missed out important information.

share|improve this answer

You ask a complex question that I frequently struggle with myself. Looking for an answer I stumbled onto your question, but I feel the answers so far don't really address what you asked. I will share my own thoughts, hoping to incite some more debate.

People post questions on SO for a number of reasons: homework, project due, learning but stumped, hobby projects, serious and deep issues, curiosity, ... And just as there are many types of questions, there are many possible answers. So the short answer to your question is

it depends

A longer answer: I use the following rules of thumb, based in part on my assessment of the OPs skills (a user whose question suggests inexperience may benefit from a more complete answer; a more advanced user may have overlooked something obvious and simply needs to be pointed in the right direction) and urgency (if you are drowning you are less interested in the correct technique for breast stroke, and more in a lifeline). But always, I hope to give the OP an answer that helps them, in their situation , and is possibly educational while I'm at it:

case 1 : no sense of urgency, trivial question

Usually asked by a Rep=1 person. I initially give a "hint" of an answer. Of the type Q: "Why does my program crash at the scanf line?" - A: "Check the function prototype. Are you passing a valid pointer?" I will often post such answers as a comment rather than answer. I think in this case, being pointed in the right direction is often all that is needed, and the OP will remember better for having done some of the work. Someone else may come along and post essentially the same answer as an answer.

case 2 : urgent but trivial

If I get a sense of desperation from the way the question is asked, then, using the same example as above, I would rewrite the single line of code with a brief explanation. The emphasis is less on learning, and more on "get me out of this hole".

case 3 : badly phrased question with some non-working code samples.

I will usually start with some comments to get clarification; if that doesn't help I may write a short "I think you are trying to do this" sample, inviting comments to improve as needed. Usually the OP just doesn't understand how to ask a good question and needs some help phrasing it correctly. A recent example that I encountered is here

case 4: insanely complex question with essential code missing

Too much code posted, yet not enough to be able to reproduce the problem, or give a useful answer. If I get the feeling it's worth trying to get the OP to an answer, I will urge the OP to shorten the question and include additional code. If the problem is interesting enough, I may try to pinpoint the crux of the question and write a short version which addresses the problem. In this case the answer is almost invariably complete code (all the way from #include to final } ).

case 5 complete code with a bug

I will often answer in two parts. In part 1 I will focus on the problem area, with one or two lines of code showing what was going wrong; and then I may post the entire code again, with the bugs fixed. This shortens the cycle of "where do I put these lines" etc.

case 6 interesting question that doesn't directly involve code.

I came across a really interesting question from NPE a few months ago. It was short, and didn't directly involve code, although it did relate directly to programming / bit manipulation. I found myself writing a lengthy answer, that kept being modified as people were commenting / posting other answers. The result was quite well received: Extracting bits with a single multiplication

Based on the above rules of thumb, I think the answer to your question is something like this:

1) No it is never "harmful" to write a complete code. When someone asks for help, and they get it, they are in no position to complain "hey that's more than I wanted to know. I wanted to try this for myself!" If that is how they feel, they should state so explicitly in their question (and sometimes they do; in that case I always honor their request for a "hint rather than solution".)

2) Is it good/bad practice? As I said above, "it depends". I err on the side of completeness; people learn from examples (and if my example is not good, comments will soon fix that; I will then take those comments into account to improve the answer)

3) Is there a standard? I don't think so - although I believe my rules of thumb are roughly the same as those that others use.

For further reading, you might want to read what Jon Skeet said in his blog, (to paraphrase: complete code is good) or refer to this earlier similar question, and its answers.

share|improve this answer

You must log in to answer this question.

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