29

Why I ask

In my work to improve questions by editing I often stumble over people who ask questions with code snippets in them. This by itself isn't a problem, since code snippets are a valid formatting option.

But I feel like code snippets are well over- and misused.

My understanding of what code snippets are supposed to be for

Code snippets, in my understanding, are supposed to be independent blocks of code or scripts that could run by themselves.

What bothers me is that they are often used in a manner of, what I understand, code samples are for.

To format dependent classes or other samples that would require context in order to be productive.

The question(s)

  • Am I wrong with my assumption about the misuse of code snippets?

  • Where can I find a clarification about how to treat these cases and what is the encouraged standard of how I should personally use code snippets or edit them in related questions?

This seemed like a question that should have an answer, but I was unable to find one.

Demo:

Example to highlight the misuse of code snippets

5
  • 3
    Sorry to break your example, but I edited out the "snippets" in favor of code blocks.
    – Cerbrus
    Commented May 8, 2018 at 8:15
  • @Cerbrus Don't forget the answer! Commented May 8, 2018 at 8:16
  • @AndréKool: Whoops, fixed that.
    – Cerbrus
    Commented May 8, 2018 at 8:17
  • @Cerbrus that was only a matter of time. though next time please edit the link in my meta post so it points to the fist version of the post mentioned :) (did that now)
    – blkpingu
    Commented May 8, 2018 at 8:22
  • This happens occasionally with C code marked up as a snippet, when there's no chance of the snippet 'run' mechanism actually running the code. Then I definitely edit to remove the snippet markup. (I haven't seen it for a while, come to think of it; maybe something changed so that people can't easily post C code in a snippet any more.) Commented May 10, 2018 at 0:17

1 Answer 1

20

Your assumption is correct, see this blog post for more information.

Not all users know how to format their code. If you come through such posts, simply edit them.

Removing the useless code snippet and formatting the code as a regular "Code Sample" is your best choice.

4
  • 9
    worth mentioning that the OP doesn't have full edit privileges and IIRC just removal of the markup comments to turn code in a non-snippet might not go well in the review queue.
    – rene
    Commented May 8, 2018 at 8:12
  • That would be the obvious choice. But in order for me to edit them correctly I'd like you to edit your answer in a manner that addresses my questions.
    – blkpingu
    Commented May 8, 2018 at 8:12
  • 4
    @rene I normally just go through and correct the spelling/grammar at the same time...
    – GPPK
    Commented May 8, 2018 at 11:37
  • 1
    @GPPK yeah, that is the preferred way to do edits anyway. Fix everything you can. If it is just white-space or changes from snippet to code (or vice versa), YMMV.
    – rene
    Commented May 8, 2018 at 11:40

You must log in to answer this question.

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