Each programming language has its own quirks, and often there emerges code patterns in the community which is collectively founded on an enormous amount of experience. Many of these patterns are highly counter-intuitive to someone new to the language (For example, quoting in Bash). Many questions on SO include code which could be made more readable, secure, reliable or otherwise "better" according to current consensus by following such patterns, but it's not a solution to the question posed by the OP. It's not a given that such advice should be introduced in SO, but it could potentially improve the overall quality of the code produced by the community, while fostering the exploration and discussion of new and existing patterns to improve even further.
There are several things one could do with questions which don't follow an established pattern:
- Add an answer with advice or improved code. This is terrible, since it's not an answer to the question.
- Add a comment. Less visible, but can't be removed after the timeout even if the code is improved. And like adding an answer, it doesn't actually improve the all-important code in the question.
- Modify the code in the question. While this could improve the question by allowing more direct focus on the problem at hand, it could also make the code longer, and there's the possibility for edit wars if the pattern is not universally acknowledged.
- Contact the user directly. This is not generally possible, and would in the best case only help one user.
- Keep it to yourself, and wallow in the code of a thousand noobs.
- Modify the code in the answer, after verifying that the changed code still answers the question.
IMO the last option is the best, since it improves the overall usefulness of SO (if applied judiciously) at the cost of moderators' time only.
What is the SO community position on tangential advice?