I asked a question that was not well understood so I found my own answer. Sadly, it needs 100 lines of code and a package from Tom Kyte to do the job. I don't see people answering questions with a lot of code here so I wondered what the opinion was?

share
add comment

3 Answers

up vote 10 down vote accepted

Large code chunks are an inherent quality of SQL. You can "golf" it a little with aliases, but for the most part database code size scales linearly in relation to the set-wise complexity of the problem. More sets involved = more code.

I've only been actively answering here for roughly a week and I've already posted some pretty lengthy code chunks. I don't do that on every question, though, and I think that's true for most members of the community. As I saw someone comment on one question, "this part is left as an exercise for the reader". There's a certain "game recognizes game" aspect to DB coding, and I will sometimes assume that if I say "Put this data in a CTE and then do an UNPIVOT on it" that the reader can take it from there.

The exceptions are:

  • I already have the code
  • I used to have the code and want to recreate it so I'll have it again
  • The questioner appears unlikely to understand the answer without a full implementation
  • The code would be exceptionally useful to future visitors to the question

Note that exception 4 is essentially what this site is about. Any question that can be answered by code ideally ought to be one where the code would be applicable outside the asker's own localized problem.

So to a certain degree, the fact that you sometimes see coding questions answered without a full code implementation is symptomatic of a larger problem: We need better questions. Good questions inspire more complete, more generalized, and more elegant answers.

Along the same lines, if your question is not well understood, that's probably a sign that it is too localized - one of the no-no's of any SE site - and would benefit from clarification and amplification to make it both more readily understood by others and more readily useful to the community in their own work.

Finally, if you found an answer to your question, by all means post it. Despite all the points and badges, the site is first and foremost a community resource, and benefits from having good answers to every question that can possible be answered. Answer your own question if necessary...we even have a badge for that. :)

share
add comment

Larges blocks of code are welcome if they are essential to the answer and valuable to the reader. There are plenty of answers on here with large snippets of code.

The rule of thumb I follow is:

  • If the code is less than a page long and it's a critical part of the answer, post it as-is in the answer.
  • If the code is really long (i.e. several pages), then regardless of importance post it to an external site like gist and link to it from my answer.

My answer here illustrates both of these points. I have a short snippet at the beginning in the answer itself, then I link to a longer script towards the end (where I say "this demo script").

share
    
i'd add one caveate that, if you link to the code, you should at least summarize what the code does, in case the link is ever broken –  DForck42 Dec 22 '11 at 20:56
    
@DForck42 - Absolutely. Answers should stand on their own as much as possible without reliance on external links (hence why link-only answers are discouraged). –  Nick Chammas Dec 22 '11 at 21:27
    
@DForck42 I'd just like to add that links to code you have posted yourself on gist, just like images posted as part of your answer (they are also hosted on an external site), are in a different category to links to other internet sites - I think they can be considered more or less as permanent as what you post on SO –  Jack Douglas Dec 26 '11 at 10:22
add comment

A large code base can obfusticate the point but if the code itself is useful it may be worth posting it. However, my experience of really large code posts is they often make people's eyes glaze over. I posted an entire database reverse-engineering script on stackoverflow once and it really hasn't got that much attention, despite having a lot of working examples of how to use the SQL Server data dictionary.

Also, there is (or was at one point) a hard 30,000 character limit on the size of postings. Relying on third-party sites to host code is also a bit hit-and-miss, as they are often blocked by corporate firewalls.

share
    
Fortunately, my solution is 200 lines of code. I've seen people's eyes glaze over at 50 lines so I'm cautious. –  kevinsky Dec 22 '11 at 17:20
add comment

You must log in to answer this question.

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