3

Suppose a user posts a programming question in SO with 20 lines of code.

Sometimes, I can give a solution to what was asked with a single line change, but I know that the overall approach is not correct. The approach I am thinking of may increase or decrease the lines of code but will be better. This would completely replace OP's code and make the question obsolete.

Other times, the OP's question was in the first 10 lines and in the remaining 10 I found a defect.

Can I suggest changes that don't directly answer the question? Although my feedback will not answer the question it will improve the code.

I know that I can post it as a comment but formatting code is not great in there.

0

3 Answers 3

4

Answers should answer the question. In the first case, try something like:

Your error message is because you forgot to add one (or subtract one, or initialize x or whatever) on line 7. [show corrected line of code.]

But in the bigger picture, reading the file [or whatever] like this is very inefficient and will not scale. Please look at the xyz function/class/framework/library as a replacement for [the inefficient thing OP is using] if you intend to have more than 10 users [or handle files over 1k, or whatever.]

You're sure their approach is wrong and not to a "coding standard" but you don't know what the app is being used for. It's possible what they are doing is fine in context. It's also possible it's not. If you actually answer the question as a sort of "price of admission", you can include other advice like "don't do it that way" but such advice is better if it's precise and bounded. It's better to say something is slow for large files, or won't handle large numbers of requests, than to say it doesn't meet a coding standard.

You can use a similar approach to say "and what's more when you fix line 7, you are going to have a problem with line 14 because it's missing a comma' - or whatever.

If you don't have an actual answer for the person, just want to say "don't do it that way" or "it's not causing this error but your line 14 is just horrible" - well, that is why comments were invented. And if you can't comment yet, then try to be patient until you can, and don't misuse answers for comments. (But remember, if you answer, you can include comment-like material in your answer as well.)

4

As long as you answer the question it is okay.

Questions do have a well-defined scope they have to follow. Answers not. Answers just have to answer the question which was asked. After that, you are free to elaborate on your answer by explaining some improvements or redesign of the logic which makes it an overall better solution.

Keep in mind that the 'extra' parts should not be the only thing in your answer. You will still be judged on your answering of the question too, so it is well possible that if you give a lousy solution for the actual question, you still might get downvoted for that. Also keep future visitors in mind, so make clear where your 'extra' section begins and ends.

3

In my point of view the best thing to do is to answer the question exactly, it may help someone with the same problem in a a not flawed approach later.
Once done with the immediate problem, if you think the rest of OP's code can be improved or has a large flaw, offer a better alternative.

This way you solve the actual problem, which someone may find later specially, and you also inform future readers of betters approaches to tackle a specific problem, making your answer even more useful.

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