I'm wondering whether answers that involve some guidance on how to use an API or sketch an algorithm should be fully functional. I often consider the answers to be pseudo-code that guides the OP in the right direction.

Would be the expectation that any posted code as answer should be "compile and run" level?

I often see OPs complaining that the provided code does not compile for some silly syntax error, while it does provides the right answer and will work with minimal effort from the OP.

Just like we expect the OP to show effort while asking a question, should we expect equal involvement when processing an answer?

  • 3
    Depends, I'm often giving fully working code (as results of my experiments) as a link to an online IDE, while just pointing out the essential parts necessary for correcting the OP's sample in my answer. I'm mostly doing so, to avoid such annoying 2nd level questions from the OPs. – πάντα ῥεῖ Nov 7 '14 at 17:26
  • 29
    Normally when I see an OP complaining that the code in an answer doesn't compile due to a syntax error, it's doomed to end up a chameleon question... "oh this compiles now thanks for fixing that but now I'm getting this other error..." – eddie_cat Nov 7 '14 at 17:28
  • 8
    @eddie_cat And even worse, I often see OPs editing out the primary error in their original question. – πάντα ῥεῖ Nov 7 '14 at 17:29
  • 1
    Depends on the question. Most of the time, with a good answer, I doubt it matters. – Joe Nov 7 '14 at 17:31
  • 6
    Ideally, but not required. If you are just pointing out a syntax error in one line, it seems silly to repost the rest of the code just so it "compiles and runs". Often I would not want some parts the rest of the code to be associated with my name for one reason or another. – Ňɏssa Pøngjǣrdenlarp Nov 7 '14 at 17:32
  • Yes, and even more if your answer is about sorting – Kroltan Nov 8 '14 at 10:00
  • 1
    I don't think it can be a "good answer" without working code. A lot of the time it is total beginners asking for code - if the code is broken or won't compile, it doesn't help them. Maybe that makes me old-skool. – Richard Le Mesurier Nov 8 '14 at 11:15

12 Answers 12


There is no general rule. It is something that each person needs to decide based on the specifics of the question.

If you feel that a high level description of the problem, or enough code to demonstrate how a solution could be written is sufficient, then that is your call to make. You may also feel that a particular answer is just a little too high level, that the missing pieces are just too complex in this specific case, and therefore consider the answer incomplete/unhelpful. If you feel that a question merits an entirely complete compilable answer in the specific context given, that is also your decision.

This isn't a strictly boolean decision. There's a sliding scale of exactly how much detail you feel is necessary to answer a question, and how much is reasonable to be left as an exercise for the reader. Note that this applies to both the actual code of the answer as well as the accompanying description. We can't write an entire book as an answer for each question, nor is it helpful to leave readers constantly feeling like they still don't know the answer to the question after reading your answer. Finding the appropriate scope and level of detail for any answer is hard, way too hard to come up with any sort of general rule beyond, "use your best judgement".

This is a decision that you need to make for each post, both when writing your own answer and when evaluating other answers.

  • 11
    "Use your best judgement". That just cannot be emphasized enough, especially when someone asks for a definite law ruling out using common sense. – Deduplicator Nov 7 '14 at 17:59
  • 2
    I would also add that for many questions, a fully compilable answer is unhelpful. If the OP is asking for a high-level description, or clearly knows how to use the framework or library in question but is just fuzzy on a couple of details, a briefer answer cuts out a lot of unnecessary noise. – Kevin Nov 10 '14 at 5:50
  • In answers, I'll use the phrase "something along these lines" to indicate pseudo code and "try the following" for code that is supposed to work. I haven't gotten many chameleon questions with this approach. (@eddie_cat -- that's a great term!) – fearless_fool Nov 10 '14 at 14:08
  • "This is a decision that you need to make for each post, both when writing your own answer and when evaluating other answers." -> like this!. Use the not-so-common common sense. – maasg Nov 11 '14 at 14:19
  • How about editing the answer with correct code after it helped you? Seems polite and useful and splits the work up between the answer person and the edit people. – Dirk Bester Mar 31 '15 at 21:56
  • @DirkBester No, that's not appropriate. If you have your own answer (possibly based on another answer, possibly not) that you feel is something that other readers would really want to see, you can post your own answer. Editing a bunch of content into someone else's answer is not appropriate. – Servy Apr 1 '15 at 13:55
  • @Servy Please read the OP post: "I often see OPs complaining that the provided code does not compile for some silly syntax error, while it does provides the right answer and will work with minimal effort from the OP." I do not see how fixing syntax errors is wrong. Posting a syntax error fix, or making almost real pseudocode work, really does not warrant a new answer. Imagine a thousand other people coming after you. Should they all suffer the syntax error? Should they all have to read duplicate posts with syntax fixes? – Dirk Bester Apr 1 '15 at 22:09
  • @DirkBester If the original author's intentions were clear, and they simply made a typo or other mistake, and you are correcting their post to what they had intended to write, then that's an appropriate edit. If the post is as the author intended, and you simply think that it's wrong/incomplete/etc. then you should not be editing it. If the author wanted to post pseudocode, and not compilable code, then that is their decision. You can indicate how useful you think it is by voting on it, but it is not your place to just re-write the code into what you think is a better solution. – Servy Apr 2 '15 at 13:55
  • @Servy Good, you finally agree with what I am saying. Also you are disagreeing with things I did not say. So we see eye to eye, hurray! – Dirk Bester Apr 2 '15 at 23:02

When I'm posting a code that might not compile and/or run just to illustrate the suggested approach and/or principle, I clearly mark it as such. For example, "the code below is completely untested".

While in many cases I do not accept the responsibility of providing OP with the fully functional code, whenever it might not be the case I disclose it upfront.


Just want to build upon what other people said that yeah it's a case-by-case basis but really make sure your code is moving in the direction of the user's ideal solution. If it doesn't, it is important to mention anything you are assuming, any limitations you foresee in the user's ideal solution and how your example addresses them. Taken from FAQ


When you make completely compilable code, I feel it leads to many users to simply copy/paste without understanding how the code works. This isn't always the case and I don't think we should go assuming that (assume the best?) but I think it becomes a problem if your code may lead the user in a different direction than they originally intended.

So bottom line: be clear what your code does and how it addresses the user's question. If you assume anything, make that clear as well.


Usually either you cannot give a full runnable solution because part of the requisites were not specified (just asking for a detail for example) or wrapping the solution into a runnable is outright trivial and would not improve the answer much. There may be some cases in between where the answer might profit from a environment that makes the code easy to execute but is not trivial to make yourself.

However if you mean by "compile and run" that the given code (fragement) should be free of errors and fully answers the question, then the answer is clearly yes. If the answer does contain errors it does not fully answer the question and is not fully helpful or useful. Leave a comment, edit it yourself, downvote, ...

And since it's a good idea to test code before you give it away as an answer, you probably have to write a bit of execution environment yourself anyway - so you can as well provide it with the answer. Also it makes testing by others much easier.

If I see two answers that differ only in the amount of helping code/comments and I have the impression the additional code/comments are valuable I will prefer the extended answer.


As a novice sometimes I want the full code but more often than not I just want some guidance on how to proceed, or just a skeleton answer so I can build on it.


If I'm supplying code in an example, I try to ensure it's:

  • Good coding style.*
  • correct
  • illustrates the point I'm trying to make.
  • clear as to what's going on.

That doesn't have to mean runnable code, but I generally think it helps. It's far easier to understand an example someone's just given if you can run it, see what it does and then fiddle with it, rather than having to figure out why you've got an error when you try to run it.

Exceptions might be when I'm 'fixing' a line in an existing bit of code, and am offering a subset (subroutine, amended line, etc.). They still need to be as good as I can make them, but there's no point reproducing their question with a couple of lines of differences.

* for some values of 'good style'. I have a particular style of code layout I use. And there's also some functions/methods that are out of date/bad practice and so should be avoided in answers.


I'd say improving/ensuring the code you're giving in an answer to be compilable and working, isn't such a bad idea.

As we discussed in comments, otherwise it will tend that OPs just ask for silly subsequent errors, or will fix their original post, and have created a totally different situation.

As we're usually require MCVE's for questions asking about code, it would be just fair giving one yourself (at least as linked online compilable code) when answering IMHO.

It depends on the actual case, what can be considered as an MCVE. If the code sample is close enough, I sometimes decide, to just make the effort to complete it in an online IDE, and point out the fixed parts in my answer along with the link.

That's not applicable (or even necessary) for all of the cases of course, but mostly for questions, that are asking about compiler errors.

  • MCVE: Usually required but not often provided. – maasg Nov 7 '14 at 17:41
  • 3
    @maasg "but not often provided." That's why I'm simply going to close such questions mostly, instead of giving an answer (though I spot what's necessary to solve it). Poor questions of that kind don't improve the site's quality in the long term, and are unlikely to help other researchers. – πάντα ῥεῖ Nov 7 '14 at 17:46
  • 7
    If a question does contain a MCVE it makes it that much less likely that I need to provide one in an answer. If the code in the question is complete, and only has a problem on one or two lines, I can post the corrected code for just the relevant snippet. The less complete the example of the problem is, the more complete an answer needs to be to demonstrate how to apply the fix. In my experience, the less code found in the question, the more code is needed to answer it. – Servy Nov 7 '14 at 17:48
  • @Servy Well, as I stated: I'll take it to an online compiler, check what needs to be fixed, put those parts to the answer, and a link to the online compilable code. It's not required to work this way of course (but very helpful for some questions I decided to answer). – πάντα ῥεῖ Nov 7 '14 at 17:52

When you answer any Stack Overflow question, you are teaching somebody something about programming. In fact, you are teaching the entire world something that you know about programming.

How would you feel if you attended a programming course, and the professor showed code that doesn't compile, or doesn't run? Stack Overflow is the exact same thing.

If you can answer the question without showing code, then that is worthwhile. If it's necessary to show a small amount of code, then go ahead and do it, but you should make sure that the code that you show is correct, even if it's not a full, working program. As well as being correct, your code should really adhere to whatever the standard coding conventions are for whatever language you're writing.

And if you need to show an entire program in order to illustrate your point, then you really ought to test it, to make sure it does what you claim it does.

If you don't have time to make sure that your code is correct, then it's best if you don't include it in your answer. The Stack Overflow community doesn't benefit from learning something that's not correct. You also risk getting downvotes.

  • Walla, walla? – πάντα ῥεῖ Nov 9 '14 at 21:43
  • Sorry @πάνταῥεῖ I have no idea what you mean by that. – Dawood ibn Kareem Nov 9 '14 at 21:51
  • +1 for analogy to a programming course. Disregarding a language's naming conventions or using non-existent constructors/methods (yes, I've seen answers with those) undermines both learning and the answerer's credibility as much as an English-as-a-second-language professor consistently speaking with poor grammar and mis-used words. – newcoder Nov 10 '14 at 6:09
  • 1
    I have never expected a professor to write compiling code on the whiteboard in a class. Despite that, I have learned from professors writing code on whiteboards in class. While having code that compiles and solves a problem does help at the monkey-see monkey-do point of learning to solve a problem... that is only a narrow band. – Yakk - Adam Nevraumont Nov 10 '14 at 14:06
  • There's a major difference between an error in the core logic of a solution and a minor error, perhaps a typo or other trivial and incidental mistake that is not germane to the core of the answer, or that is trivially found and fixed by the audience of the post. Yes, such errors should be fixed, yes, such an error does make an answer worse, but an answer can still be a great answer even if it's not 100% perfect. – Servy Nov 11 '14 at 15:45

No, code in answer may not compile and have no reasons to show all best practices of coding. The only real requirement is to demonstrate the answer.

Should such answer receive downvotes - if you feels so, but the fact one can't copy/paste answer into code and hit the ground running is not really the reason to do so.

Can such answer get upvotes - likely if it shows direction that is not obvious by itself but completely clear when you see approximate sample code.

Is such answer "not an answer" - no, "very low quality" - probably not (unless there is just one line of bad code).

If I plan to write such answer I:

  • expect downvotes
  • expect someone else to come up with clean sample as separate answer as copy of mine. Or even edit mine (also later is unlikely due to "don't edit code" policy on SO)
  • consider eventually fixing up the code (if answer gets comments/up-votes)
  • consider eventually deleting answer if there is better one

Note that to require perfect production-ready code as answer would require large tax on person answering the question and people who follow the rules should stop answering at that point. On other hand production-ready samples would solve problem with "do my homework" questions as code will not be acceptable as school/college answer.

One would need to do some all all of following partial list:

  • makes sure all exceptions properly handled
  • logging is setup and used as needed
  • all text ready for localization
  • number/time parsing and formatting uses correct locale, including discussion on digits/numbers outside of ASCII range...
  • performance concerns mentioned and alternative versions shown that work with different performance constrains
  • security concerns shown and recommended practices linked/shown
  • unit tests cover main functionality
  • provided versions for one or more major flavors of coding guidelines for languages in questions



Optimum would be code that starts with <html>, and ends with </html>. It's great to see the code actually work on screen. But, for long programs or questions in which a snippet should suffice, the snippet would be acceptable. Often, one can tell from the question if the OP would know how to insert the snippet to make it work. So, if one can address the question without an overly lengthy answer, yes. But, in most cases, the OP should be able to implement a snippet of code.


You should provide a working solution, it could be a complied working code or a working pseudo-code or a working high-level solution depending on the request and the question. Syntax errors or typo could be OK, but anything which leads to a not working code is your responsibility.

Notice sometimes you can not be sure your pseudo-code works until you implement it.

  • 3
    If you can run pseudo-code, is it still "pseudo"? – maasg Nov 9 '14 at 8:59
  • Yes, code is just the translation of pseudo-code, pseudo-code must be clear enough to be translated to a working code (it can omit obvious parts), you can regard it as an algorithm, algorithm must be proved that works. pseduo-code can omit some steps but can't have a wrong step – Ahmad Nov 9 '14 at 12:18
  • 1
    I meant you either should be so sure about your pseudo code or if you are not you can get sured by implementing it. – Ahmad Nov 9 '14 at 12:20

100% it should work. This is a place to seek help and guidance. If the OP's are constantly bashed for not providing x, y, z..

It only make sense to hold the answer providers to the same standard. (especially when they get the reward/points..etc) Its much to easy to provide a link out of context, no comment, a suggested idea and leave with a snarky comment ...then get rewarded with points & upvotes.

Take the time the OP has to take to provide and explain things, or pass the post up. In most cases someone is looking for help/guidance, passing the buck with a simple link and no help/comments attached is the bare minimum of help IMHO. Non-working examples or pseudo code often just lead to more confusion and questions.

  • 1
    I've seen working code leading to blind copy/paste without understanding and that becomes very visible when the there're minor errors that the OP does not seem to be able to overcome. – maasg Nov 9 '14 at 23:17
  • 2
    I think copy-pasta code recycling is a horrible thing, but can't deny that I've had a lot more success building on a 'known good' example than I have a partially working snippet. – Sobrique Nov 10 '14 at 13:46

You must log in to answer this question.

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