How to answer programming questions online

  1. Find a duplicate
  2. Perform a sanity check
  3. Do not answer using guesswork
  4. Offer a generalized solution
  5. Use best practices
  6. Test your code
  7. Keep your answers up to date
  8. Comments (3)

There is one thing that people participating on Stack Overflow often do not realize:

When answering, one is helping not only the user who asked question, but also thousands other people who will come from Google search later.

While everyone is trying to fulfill the wishes of the particular OP, the numbers are against them: the OP will forget this question the next day, while other people will keep coming for many years.

So in the light of this simple equation, one can formulate the basic principles for answering:

Find a duplicate

Before answering, look for a duplicate. Most likely this question has been answered already and it's better to link an existing answer than to write another one.

Perform a sanity check

always perform a sanity check for the question. People make strange assumptions and draw even stranger conclusions. Always verify the claimed behavior against your knowledge. If you don't have enough knowledge in the area, then don't answer at all, but wait for more experienced users to come.

Do not answer using guesswork

Avoid blunt suggestions like "a file cannot be saved? check permissions!". There are thousands possible reasons for such a problem. Even if your suggestion will work for the OP, there are others for whom it won't. First tell a user how they can get the error message that will reveal the actual problem, and after that offer them the solution for this particular issue. Consider editing the question to include the error message to help future visitors quickly identify whether the question applies to their case.

Offer a generalized solution

When answering, always prefer a generalized solution for the problem stated in the question title, as Google will send people from Search based on the title.

Use best practices

Always offer the better solution, follow the best practice known for the moment. Avoid quick and dirty hacks. People would code and copy/paste your code without thinking twice, so don't make Stack Overflow a dispenser of bad or outdated practices. Do not submit to low demands. Remember all those people who will learn from your answer.

Test your code

Always keep handy a test bench, an environment for the chosen language, where you can test the code you are offering. It will save you from making typos and also from a shameful blunder in case the technology is not that familiar to you. In short, always test your code before posting - it is not as hard as it seems. For example, every PHP user has a local web-server with PHP and database installed. It does no harm to write your code in your favorite editor, then run it and only then paste it to the answer.

Keep your answers up to date

If you notice that some answer of yours is getting more attention than others (in other words it gets an upvote once in a while), revisit it regularly and keep it up to date. One good, refined and well-explained answer is better than a dozen of quick and dirty code-only snippets.