22

If we encounter two different questions which have very similar answers, is it appropriate to answer using code from our other solutions? Naturally, the solution should be described in the context of the question it's answering, but if the code is the same (altering variable names where necessary), is there any reason not to lift it directly out of an older answer and post it to the second question?

Going a step further, is it legitimate to copy other peoples' solutions as long as we properly credit their work from the source thread and don't lift their words verbatim?

I feel like linking the solution in a comment doesn't really convey the idea as a proper answer (since a full answer post can describe how it works), and posting just the link does not constitute a proper answer.

I've seen a very large body of questions and answers here on Meta about submitting duplicate answers to the same question thread, but I haven't been able to find anything to do with (appropriately) reusing old answers for different, nonduplicate questions.

To some extent, this seems natural and right, since there are many situations that can share an efficient solution, and in fact a particularly fast and elegant expression will be applicable to a lot of different situations. But does SO policy agree with that? Thanks.

9
  • 11
    If the questions have the same answer, it would seem they're duplicates. Do you have specific examples where this is not the case?
    – Ken White
    Commented May 31, 2015 at 2:20
  • 5
    I did but I've mislaid the links. In the meantime, consider the hypothetical questions, "How can I sort these tuples by the value of element 2?" and "How can I sort these strings using their third character?" Both can be accomplished using sorted(inputSequence, key = lambda i: i[2]). The two are similar, but not duplicates. (Obviously, the answer text would have to be modified to suit the question as-asked, but the basic idea is the same.)
    – Augusta
    Commented May 31, 2015 at 2:46
  • @KenWhite ^ (I mean, the sorts of questions we're liable to encounter are more sophisticated than these examples, but you get my meaning, yes?)
    – Augusta
    Commented May 31, 2015 at 2:53
  • I haven't seen such question pairs in real life yet. Commented May 31, 2015 at 18:00
  • @DmitryGrigoryev I've seen a couple. They aren't really that uncommon, especially if you're looking for them specifically. I guess that's partially what tags are for.
    – Augusta
    Commented May 31, 2015 at 20:47
  • @Augusta I would close those two questions as duplicates of each other. The same answer works. They are really (in that language) about objects with an associative map and sorting. Commented Jun 1, 2015 at 20:14
  • @Yakk Okay, fair enough, but I would only close one of them, and for the purposes of this conversation, let's pretend that I came up with a better example, okay? ;)
    – Augusta
    Commented Jun 1, 2015 at 20:37
  • That is the problem: I'm not sure there is a better example. If you are duplicating your answer, and the answer doesn't suck horribly, then in every actual case I've seen one should be closed as duplicate of the other (not always both ways). In the cases where the answer sucks, it could technically answer both without being a good answer to either: but in that case, duplicating the answer is also a bad idea. Stack overflow is about answering practical problems. We should apply some of that filter to MSO issues as well. Commented Jun 1, 2015 at 20:47
  • @Yakk Okay, but not believing something exists is a different thing from its actual non-existence. If you won't take my word on it, that's totally okay, especially because I can't find my evidence at the moment. But with that in mind, let's just treat it as a hypothetical situation and move on.
    – Augusta
    Commented Jun 1, 2015 at 21:54

1 Answer 1

13

As I understand it, original code posted as part of answers to SO are licensed under a Creative Commons license. This addresses the question of whether copying is allowed (i.e. properly attributed, yes).

Now, as for whether this is a reasonable way to answer a question? Sure, I don't see why not. Taking as granted that the question really is different, but code found in another question (either in the question itself or in one of its answers) is still applicable and useful, not only is it fine to copy the code, it would be silly not to.


Of course, you should be very careful when you do this. It seems to me that being able to copy/paste code from another question is a strong indicator that you are dealing with a duplicate question. Taking the example you describe in your comment above:

"How can I sort these tuples by the value of element 2?" and "How can I sort these strings using their third character?"

It seems to me that those two questions really are the same. I.e. while they are presented using specific examples, the real question is "how can I sort a sequence on some arbitrary key found in the elements of the sequence?"

If we consider those two questions not duplicates of each other, then we should (for example) also consider "how do I add 1 and 1 together?" to be a question different from "how do I add 1 and 2 together?".

Just because the person asking the question wasn't able to generalize their presentation sufficiently, that's no reason for us to have umpteen zillion different instances of answers to the same question. We can safely generalize on behalf of the questioner and resolve those as duplicates, even though the specifics of the question as presented are different.

If you feel the person posting the new question might have trouble generalizing, you can help them out. For example, include a brief comment to help the questioner understand why they are duplicates and how to generalize the answer. Or even better: if one is not already present, add a new answer to the original question in which you rephrase the question as the more generalized form and provide an answer that is more clearly useful (i.e. instead of just doing their work for them, teach them to fish :) ). Then you can add a comment to the new question directing them to that answer specifically.


But all that said, it seems plausible to me that one could run into a situation where the same code is applicable to two genuinely different questions. E.g. where the code is just part of the solution, but is an important and useful part which when combined with some other code that is unique to the answer to the question at hand provides a useful answer to the question.

In that case, copy away. :)

3
  • 2
    The questions are the same in that they deal with similar situations, but the sort of situation I'm describing (or trying to describe) is one where two questions deal with situations sufficiently different from each other that they're unlikely to share a natural search phrase. "How do I add one to one?" and "How do I add one to two?" can be reduced to "How do I add two numbers?" while the example (which I concede is way, way too basic and is not very good) deals with strings and tuples separately. You and I know that they're both iterable, but it's perhaps not that obvious to a neophyte. ;)
    – Augusta
    Commented May 31, 2015 at 20:54
  • I mean, my example questions, rather.
    – Augusta
    Commented May 31, 2015 at 21:04
  • 13
    I think it's probably not so uncommon to have two fairly distinct problems coming from a single root cause. "I'm getting error X" and "I'm getting error Y" might not be duplicates, while an answer like "Feature Z should be initialised by using this when your application starts" would apply to both. Especially when using third party libraries in a complex system, sometimes, problems can manifest themselves in very different ways, leading to non-duplicate questions.
    – Bruno
    Commented May 31, 2015 at 22:55

You must log in to answer this question.

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