Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 158 Stack Exchange communities.

What is meta?
Here's how it works:
  1. Any Stack Exchange user can ask a question
  2. The community provides support, votes on ideas, and reports bugs
  3. Your voice helps shape the way Stack Exchange operates

A thing that I noticed a lot recently is that if you try to answer a question in a way that makes clear what is really happening (being uber-verbose, maybe instantiate one or two objects too much, doing things in two steps where one would be possible as it gives you room to explain what's happening, having lots of comments in the code) as you have the feeling that the OP is a novice and might want to learn what is really happening in the lines he is writing, you will often attract comments like: That was fine (other than the bunch of unnecessary temporary objects) or other semi-snarky remarks complaining about calling a jQuery-selector once too often (I don't mind these comments at all btw).

Although I don't mind them it led me to wondering if it is encouraged to always use best practices (as far as they may exist) in examples, even it may confuse the OP or encourage him to just copy the code and run (as he will not understand it due to its optimized nature)? Or should one just try to "get the message across" even if it may hurt a little (I'm not talking about writing code that is plain wrong...)?

I'd opt for getting the message across by the way as I don't think SO should be a place where you get production ready code (but instead improve your skills by understanding things you haven't understood before - I have learned so much - in exactly this manner - around this place and still am), but I could also see reasons for advocating the former. Is there some kind of community agreement on this topic?

share|improve this question
up vote 3 down vote accepted

I think it depends on the question. If the question is about a best practice and you want to post a code sample illustrating your answer, then by all means post the code needed to do so.

In general, however, I my general rule of thumb is to post the minimum amount of code needed to illustrate my point and nothing more, even at the expense of best practices like good comments, proper error handling, clear method/variable names, etc.

The important thing is to keep your answer as to-the-point as possible while still answering the question. You can't teach everything at once, so you have to do it in small chunks and assume that the OP will learn the best practices lessons from questions about best practices.

Of course, it never hurts to mention (perhaps in a footnote) possible best practice or other issues related to the samples in your answer, but it shouldn't distract from the core content of the answer.

share|improve this answer
+1 for: assume that the OP will learn the best practices lessons from questions about best practices. – m90 Mar 23 '12 at 16:42
Obligatory snarky comment: Except that questions about "best practices" aren't on topic here and are inevitably closed by the despotic moderators, preventing anyone from learning anything. Ever. – Cody Gray Mar 23 '12 at 17:07
@CodyGray - Obligatory snarky response: Not all questions are posted on Stack Overflow. Not all question are even posted. – cdeszaq Mar 23 '12 at 17:08
@codeszaq should I (Poster of question) should add code in my question? does having lot of sample code in the question distracts experts (in that particular subject)? – shankbond Jul 16 '13 at 3:16

You must log in to answer this question.

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