For example, if the OP asks why the array arr is not full in the following code:

for (var i=0;i<10;++i) {
  var arr=new Array();

Is it better to simply say "put the arr initialisation outside the loop", or to totally rewrite the poster's code to do it for them?

The reason I ask is that it makes sense to me to just suggest an answer and have the poster learn something by figuring it out for themselves.

If you just give them the answer on a plate, they will expect that in future, possibly even asking the same question again because they didn't learn the first time.

4 Answers 4


I think it depends on who you are answering to.

If you can see that it is a total beginner who needs things to be explain step by step, then rewrite his code, showing and explaining to him where things can be improved.

If you have to deal with an experienced user (who is just probably too tired or something. I guess you can have an hint on his skills if you check his questions and answers), then just a simple formal explanation should be enough.

  • Well, i've more than 10.000 reputation on stackoverflow (mostly from javascript/jquery/php) and now i'm working on Java/Oracle: sometimes i get simple formal explanations when i'd like the full code!If i ask a question i'd like a precise answer from which i could learn Commented Nov 16, 2011 at 8:48
  • Yes that's why I proposed to check the questions and answers too. I agree probably the reputation is not a good factor, I'll remove it.
    – talnicolas
    Commented Nov 16, 2011 at 8:49
  • But really the point is "Try to know as precisely as possible who you are talking too" :)
    – talnicolas
    Commented Nov 16, 2011 at 8:52
  • 3
    +1 know your "audience" by looking at other Q & A...make a decision based also on sensing for explicit or implicit [homework] according to your alignment with that. Remember also that in the long tail it's probably more search engine hits coming along looking at the question than the OP ever will, so be clear for their sake too. Commented Nov 16, 2011 at 8:52
  • good points, and I think I agree. If the poster makes fundamental mistakes in the code, it's ok to rewrite the code completely and explain why, but if the poster appears knowledgeable, then a simple "the problem is ...", ignoring any "inefficiencies" in the rest of the code should suffice.
    – Kae Verens
    Commented Nov 16, 2011 at 8:55
  • I think so yes :)
    – talnicolas
    Commented Nov 16, 2011 at 8:56

The obvious and simple answer is "of course it's better to just show the way".

However, things are not always that simple. The OP might not understand something like "put the arr initialisation outside the loop" - either language barrier, or lack of experience. (or both)

So in my opinion posting code is usually good idea, no matter how trivial it might look to you.

Now if you post such code:

var arr = new Array();
for (var i = 0; i < 10; ++i) {
    arr[i] = i;

It's not really "rewriting" the original code, just fixing the specific issue. Usually that's what I would choose, but sometimes it might be good idea to indeed rewrite the code giving the best practice, for example:

var arr = [];
for (var i = 0; i < 10; ++i) {

Well usually I think the best thing is to rewrite the OP's code (only if it needs rewriting) and explain carefully why I have rewritten it. This is because when I was answering question I wanted an answer that worked, not an hint. Sometimes the OP isn't very skilled and even something simple like "put the arr initialization outside the loop" could confuse him so I take the array outside the loop and explain why I do it.

I don't think I'm not answering question to "teach" something (I hope to do so, but that's not the point), I answer question to solve problems.

  • +1: You are still teaching them this way. I have taught classes before, and when teaching face to face you can guide someone more gently as Kae said, but when on StackOverflow you should aim for completeness and clarity. In person you can gauge their reactions from hints in the right direction and the communication is realtime. On SO, since we lack this face-to-face one-on-one environment, the way you answer changes. Commented Nov 16, 2011 at 13:18

It's a must to answer the question, explaining what is wrong and pointing out solutions.

Bonus points if you do the work for the OP.

  • "Bonus points if you make the work for the OP", Do you mean "Bonus points if you do the work for the OP" or "Bonus points if you make work for the OP"? Because -1 if it's the second ;-) Commented Nov 16, 2011 at 13:20
  • @TheUnhandledException: Grrr...yes, I meant the first. Thank you. :) Commented Nov 16, 2011 at 13:21

You must log in to answer this question.

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