This question already has an answer here:

When it comes to writing code for a question that asks about a procedure, would it be better to write pseudocode, or to write general code in the language they ask for? Some questions, like comparing strings or some such, would obviously require exact code. Outside of that, is it better to provide a text answer saying what is wrong and how to fix it, or to provide code saying it?

share|improve this question
add comment

marked as duplicate by Cody Gray, hims056, Tobias Kienzler, Łukasz 웃 L ツ, Martijn Pieters Aug 8 '13 at 11:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 2 down vote accepted

Similar to Robert and Oded's answer, I'd offer the rule of thumb I use when answering questions here and elsewhere:

If a question is asking for a specific method of doing something (normally questions like 'How do I do x in language Y', 'how to use language-specific feature Z' or simply 'A is not working in my code') then a clear answer in the same language is better, especially when accompanied by a sentence or two explaining why your specific approach is a good one (why one library is better than another for the task at hand, or what error you've fixed in the original code)

If a question is asking about an approach, or or an algorithmic nature as Robert noted (e.g. 'What is the best way to do X' etc.) then a general answer is almost always better unless they have specifically requested a code snippet as figuring out how to apply that general advice to their language/situation not only helps them learn and allows them to feel as though they've achieved something themselves (and not been spoon-fed a solution and still not fully understanding it), but they can also apply that knowledge to similar situations - things you don't get from "try this code instead".

Ideally, of course, a good answer would contain enough information explained clearly enough that it actually ticks both boxes by providing an answer that is applicable to the problem that caused the question while being useful to someone facing the same issue in another language, but that can often make the answers much more difficult and longer to come up with. Similarly, there are a number of areas where there is no clear boundary between the two types I've outlined above but that is one of the reasons that we ask our questions t people and not machines - we can tailor our answers to help the asker in different ways.

It may seem like I have stated nothing new, but I have found that spending a moment or two thinking about what the questioner really wants to know (often not exactly what they are asking) helps prevent the questioner sending both of you back to square one with 'Sorry, I still don't understand'.

share|improve this answer
add comment

It's almost always better to provide real code, unless the question is of an algorithmic nature and the OP has specified that answers should be language-agnostic.

If the OP has tagged the question with a specific language (by far the most common scenario), write your code in that language.

share|improve this answer
add comment

would it be better to write pseudocode, or to write general code in the language they ask for?

Better for who? For the asker, I can imagine that in the same language would be best.

However, for everyone else? It depends on the question - if you can give a good answer in pseudocode, give it in pseudocode - this allows most people to understand and implement in their language of choice. This is a good candidate to illustrate an algorithm - to explain a point without getting bogged down by the exacting requirements of a compiler.

Outside of that, is it better to provide a text answer saying what is wrong and how to fix it, or to provide code saying it?

It really depends on the question - not all answers are better by having code in them. Explaining in both code and words is best - sometimes adding an illustration makes the point even better. There is no way to give a blanket answer on which is best - it all depends on the question, on who you are targeting in the answer (level of knowledge etc...).

share|improve this answer
add comment

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