20

I've created answers that contain a runnable code snippet to provide a convenient method for readers to view a demo of my answer's solution. For instance:

enter image description here

In my opinion, this answer:

  • highlights solution in first few lines
  • provides code snippet to run and view solution in context
  • collapses code snippet for readability to allow focus on solution above

However, a more experienced user edited out the snippet without a revision comment, leaving the code expanded. Given that this has happened on a few of my posts (incidentally edited by the same user), I'm starting to think that code snippets might be frowned upon, so I'm interested in clarification on when to use/avoid them. Thoughts?

15
  • 3
    Did it do anything when run?
    – Laurel
    Commented Apr 23, 2016 at 16:41
  • Yes, it demonstrated the solution I provided in the preceding lines.
    – user600838
    Commented Apr 23, 2016 at 16:50
  • 5
    No, does the code do anything? Will it actually run, given that Stack Overflow probably doesn't have iron-flex-layout/iron-flex-layout-classes.html laying around to be imported? If not, it shouldn't be a snippet -- snippets are for live, working code samples.
    – Paul Roub
    Commented Apr 23, 2016 at 17:00
  • 11
    Yes, that's what I mean by "demonstrated" (i.e., shows code in action when the user clicks "Run code snippet"). The code snippet contained the appropriate HTML headers to successfully import iron-flex-layout-classes.html.
    – user600838
    Commented Apr 23, 2016 at 17:04
  • 6
    It's likely that the editor -- like me -- didn't notice/realize that (I assume) the base tag would allow the import to actually work. Naively, it looks like an example that would only work in a properly-set-up environment. But code that actually runs? Yep, valid snippet.
    – Paul Roub
    Commented Apr 23, 2016 at 17:10
  • 1
    Perhaps. As I mentioned this user is experienced -- as in he's well versed in the subject matter, so it seems unlikely for him to not fully understand the short code snippet. And it really only takes two clicks to verify the snippet (one to expand it, another to click "Run") but several more clicks to edit it out.
    – user600838
    Commented Apr 23, 2016 at 17:13
  • 9
    That's true. I assumed that the snippet is not runnable. Sorry for that. It's very common that people use this snippet thing for code that is not runnable and I find that very inconvenient because the code is hidden and I need to click to see it and the additional buttons distract from the content. I just didn't expect Polymer code to be runnable as snippet. I'm really sorry and glad that I learned this is possible. Commented Apr 23, 2016 at 18:00
  • 2
    @GünterZöchbauer no worries. I assumed I was doing something wrong, but I'm glad we cleared that up here.
    – user600838
    Commented Apr 23, 2016 at 19:34
  • 1
    @GünterZöchbauer I agree about the distracting non-functional buttons, but do you really come across many code snippets that are set to hide: true but don't run properly? Given that the default is to show the code, and you have to either edit the source or click the "hide by default" checkbox, that seems odd. Hopefully the fact that the author went to the trouble of quoting the important code separately, combined with the snippet being set to hidden by default is a good sign that the author is using snippets correctly.
    – CupawnTae
    Commented Apr 23, 2016 at 20:03
  • 1
    Your snippet clearly does run and it is a perfectly valid use of a snippet (not to take anything away from Paul, but I would at least attempt to run the snippet before editing it out :) ). As the snippet is short in this case, maybe making it hidden was unnecessary. When it is more than say 100 lines, I would hide it as it makes the answer less readable.
    – Rhumborl
    Commented Apr 24, 2016 at 9:40
  • 1
    @CupawnTae There were quite a few poorly formatted questions of beginners (< 50 rep) recently that used snippet to get code displayed. I'm not active in pure JS topics (only Polymer and Angular) and there running code snippets are quite rare. Commented Apr 24, 2016 at 11:13
  • @Rhumborl sure, that's what I'll do from now. Commented Apr 24, 2016 at 11:14
  • 1
    @GünterZöchbauer I don't doubt it, but my question was about whether you've seen many hidden snippets by these users - hiding them takes extra effort, so the people who just hit "snippet" without really knowing what they're doing (and/or are trying to show their code) are unlikely to do this. So I'm saying that perhaps a hidden snippet is a sign that it really is intended to be a snippet. And conversely, I'd be surprised if you have to hit "show" on the newbie's not-really-snippets to see the code.
    – CupawnTae
    Commented Apr 24, 2016 at 12:18
  • 1
    @CupawnTae I don't remember exactly. I'll pay closer attention in the future then I might be able to provider better feedback next time. Commented Apr 24, 2016 at 12:20
  • I didn't knew there was a base tag in the HTML spec, figures.
    – Braiam
    Commented Apr 24, 2016 at 17:22

0

You must log in to answer this question.