5

Sometimes I write multiple snippets in an answer, to show different approaches. Often, they use the same test data, which sometimes can be big.

I'd like the ability to somehow designate a snippet as being "shared", so other snippets can access its variables, and I don't have to repeat the test data in each snippet.

It would be even cooler if I could access a shared snippet from the question, so posters could put their test data in a shared snippet and everybody could access it.

1
  • 1
    The principles of good program design are quite universal. We should avoid tight coupling between SO posts as much as we should avoid tight coupling between objects/files in our programs. What if the OP changes the code? All posted answers will be outdated. Then new answers gets posted that work on the new data. This will create a horrible mess in no time at all.
    – Lundin
    Jul 6, 2017 at 13:10

1 Answer 1

26

Live shared data between Question and Answers: Don't do it

We should not have a shared snippet context between the question and answers. Doing so inherently sets up a dependency on the question for each answer accessing that shared data. This means that the functionality of the code snippets in answers could be changed with an edit to the question. This leaves open both mistaken and malicious altering of that data causing some, or all, answer code snippets to become non-functional. Someone could even edit the data in the question so that one answer worked, but another did not. Answers should be independent of each other. If multiple answers live-source data from the question, then the answers are not independent.

A Common data/code block for snippets in a single post

This is more reasonable from the point of view of Question/Answer independence. .However, given that each snippet is implemented, as it should be, in an <iframe> what you ask for is, at best, …difficult. It's possible, but I'm not sure it's worth the effort. I'm just not seeing significant tangible benefits from doing this. It sounds cool, but is it really that beneficial? It would save some copy-&-paste work, but does it really improve questions/answers?

Data sets should not be huge, that's part of a MCVE (stress on the minimal). If the data set is huge, then you should ask the OP to reduce it. If it is huge, then, yes, having multiple copies each used in a different snippet could result in getting to the 30k/post character limit faster than you desire.

I'm just not seeing enough benefit for the effort which would be required to implement this.

Use a <script> tag in your HTML for setup code, data, etc.

Personally, what I normally do is put things which I'm not trying to have the answer/code focus on (e.g. common setup code, data, hacks to make some APIs work well enough for examples, etc.) in a <script> tag within the snippet's HTML code block. That way, the JavaScript code block is left clean to highlight the code which is the actual answer.

For answers where I'm highlighting the difference in ways to implement something, putting the setup/data in the HTML code block works well. It also allows me to just copy-&-paste that entire block into a new snippet, should I decide I want to show another alternative.

3
  • I agree on Q->A code sharing. No way. And the data shouldn't be big. Yes to M(inimum)RE. But even if it's not that large, sometimes it can impact readability. I often write answers that include multiple methods, or I will start with a simple implementation and keep iterating on it with refinements to demonstrate how we can build on core concepts. I just got done with one where my methods were between 3 and 1 lines long, but the data set was 11, so user code made up 85% of my answer since I used it 5 times. And it's difficult scrolling between the input box and the preview when it's long. Nov 24, 2021 at 23:37
  • 1
    I like this, though: <script> tag within the snippet's HTML code block Nov 25, 2021 at 0:44
  • Yeah, the separate "setup code" solution sounds quote good to me, too
    – shaedrich
    Dec 17, 2021 at 8:35

You must log in to answer this question.