2

Can I post a new improved answer, based on another one under the same question(with a reference to the original answer) with the only difference, that the code snippet, which solves some common problem mentioned in the question is converted to a standalone convenient function?

Consider this example of such a question: How to check if a directory exists and create it if necessary?

What if the only answer was this solution:

try:
    os.makedirs(path)
except OSError as exception:
    if exception.errno != errno.EEXIST:
        raise

then would it be acceptable to post a new answer with this code:

def make_sure_path_exists(path):
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise
1

2 Answers 2

1

I'm going to expand on undo's answer a little, which I agree with.

The example you gave is such a trivial change that I would be unlikely to call it refactoring. As a reader, I would consider the extra, near duplicate answer to be annoying. If you're browsing all the answers, it's another thing to read, which takes a little time too (especially if you get disrupted).

It would be perfectly reasonable therefore to down vote for not being useful.

On the other hand, sometimes the underlying method of the answer is valid, but the presentation is severely lacking. (This happens more often in languages other than Python, where white space is more optional.)

In that case, a substantial enough improvement on the answer might be a good idea, especially when the original answer has problems that can't be fixed by peer-editing.

2

Depends on how you define 'acceptable'.

If you define it as "not against the rules", then yes, it'd be acceptable. As long as you properly attribute the code and where you copied it from, I wouldn't have any reason to delete the answer.

If you define "acceptable" as "likely to go over well", it's not. You're likely to be massively downvoted, get a bunch of (correct) comments about how your answer doesn't add anything, and probably rack up a bunch of flags.

In short, you probably don't want to do this. It'd probably be better to edit the answer instead, if a function is significantly more useful.

2
  • Why do you think this would be downvoted? In my scenario of a "common" problem, such new refactored answer could be very useful: a lot of people use stackoverflow to just copy a piece of code and past it into their programs. In case of a standalone function it's much easier/safer to do so, and saves a lot of time for community, because common problem questions are usually very popular (have >10000 views) and in case of an inline code snippet an average programmer could spent e.g. 1 minute integrating it into his or hers code and introduce some bugs while doing it. Commented Aug 8, 2016 at 3:17
  • 3
    @mechatroner I'm willing to bet that most programmers can wrap some code in a function in less than a minute. Not saying that it's against the rules, just that I've seen enough flags on these kinds of things to know that they aren't looked kindly upon by many users.
    – Undo Mod
    Commented Aug 8, 2016 at 3:35

You must log in to answer this question.

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