How to answer programming questions online
- Find a duplicate
- Perform a sanity check
- Do not answer using guesswork
- Offer a generalized solution
- Use best practices
- Test your code
- Keep your answers up to date
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.
- think one move ahead: what would be the outcome of my answer? Will it do any good for the OP? May be it would contradict with good practices or common sense?
- think one move behind: why would the OP ask such a question? What could be the background? Isn't a question being an XY problem? If so, what could be the actual problem instead?
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.
- Only after offering a generalized solution, add a section for the OP's problem. It will help them but not at the cost of distracting the future visitors.
- Refrain from nitpicking, and do not make your answer an irrelevant code review. It will distract a future reader from the core problem's solution. If a code review is all you can offer then don't answer at all, instead vote to close the question as it is off topic - it may be better suited to Code Review Stack Exchange.
- In case the question title is not relevant to the body, or not informative at all, edit the question title. It should be relevant to the problem and unique enough. As a result, it will attract more visitors and bring you more upvotes.
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.
- A hint: you can always use this bench to test the code posted in the question. Sometimes the OP claims that their seemingly working code doesn't work, so you can test and prove it. Otherwise, vote to close the question.
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.