9

I've asked a question about regular expressions. I've got one correct answer and a answer that shows a way better solution for my problem. Actually the answer with the better solution isn't a correct answer IMHO since it's not answering my question.

I tend to accept the correct answer.

Which answer should I accept?

7
  • 4
    Select the one that's the most useful for you. Commented Apr 23, 2014 at 15:20
  • 3
    How could a solution be way better and also not be correct? Wouldn't being incorrect make it not be better?
    – Servy
    Commented Apr 23, 2014 at 15:21
  • 1
    Upvote the one you found most helpful to you, in the specific problem you had. You could leave a comment that the other one was a very good approach but didn't answer your specific question. Commented Apr 23, 2014 at 19:56
  • @Servy see the answers to workplace.stackexchange.com/questions/19975/… One can have a great answer to a slightly different question. Commented Apr 23, 2014 at 19:58
  • @La-comadreja Then it's not a better answer, if it's answering the wrong question. It's a worse answer, for failing to answer the question at all.
    – Servy
    Commented Apr 23, 2014 at 19:59
  • it seems Lucas has been a bit unclear with the wording of the 2nd sentence but the 3rd sentence seems to explain. Other answers could be prettier responses that are relevant to Users in general, but not so much to the poster of the question. Commented Apr 23, 2014 at 20:00
  • Related / my first thought when I read this: What is the XY problem Commented Jul 11, 2014 at 14:53

4 Answers 4

6

There is a difference between answering the posted question and solving the problem presented in the question.

SO tends to be more of a database of question-solution. As such, I'll say that you should accept the answer which directly answers your question and upvote the one which provides a better alternative solution.

You have to consider that others with the same problem you had will find your question and might not be able to incorporate the alternative solutions for whatever reason (could be even that it's homework and they are just restricted to certain usages). They want an answer to the posted question, and as a bonus they can see better ways to solve the problem.

1
  • 2
    Sorry but I disagree with They want an answer to the posted question, and as a bonus they can see better ways to solve the problem.. In my experience people mostly want to solve their problem and are answers to their questions more of a bonus. Commented Jul 11, 2014 at 14:48
7

Accept the answer that best solved your problem - after all you are asking about a real problem you have, right?

Personally I would accept the one that provided the better solution. A regex type question could get a dozen 'correct' one line answers but an answer that actually solves your problem and furthers you in the process is the one that I would give the 'prize' to.

2

In general I would say that the best answer would be one that solves your problem but is also general or complete enough to be useful to other users with similar (as opposed to identical) questions. Solutions that work for a variety of situations (within reason!) are, in my opinion, superior to those that work for only a very limited, specific situation.

For example, I once asked a question about parsing simple, consistently structured HTML using Perl. One user gave a very complex regular expression that worked well in my situation, but another user encouraged me to pursue using a Perl module built specifically for parsing HTML. I ended up implementing that and learning a lot in the process. That solution helped me but I'm sure it also helped other Stack Overflow users or passers by, and I think that gets even more to the heart of Stack Overflow than just what works for you.

2

If somebody provided a better solution then what you asked for, you are like most users on any helpdesk: You dont ask for help with your problem, but you ask how to implement what you think is the solution.

I see it all the time on SO and any other forum/helpdesk I have visited and unfortunately it mainly involves the none lazy people. They did try to find a solution for their problem themself as requested/advised by SO, but they are only searching for solutions within the scope of their knowledge. And then after a few hours of messing around in copy/pasted code they ask help to implement their solution, without mentioning the actual problem.

Example PHP Problem (simplefied):

How can i parse the following string to a key/value array?

"key1=val1&key2=val2&key3=val3&key4"

Solution tried:

Explode (split) on &

Loop the resulting array and explode each value on =

save the results to an array

<?php
$str = "key1=val1&key2=val2&key3=val3&key4";
$ar1 = explode(";", $str);
for ($i=0;$i<count($ar1);$i++) {
  $ar2 = explode("=", $ar1[$i]);
  $result[$ar2[0]] = $ar2[1]; //error for key 4 since there is no =
}
?>

Question asked:

Why do I get a warning that an index is undefined

Awnser:

You should use array_key_exists

Now the question is answered, since there wont be a warning any more. But if he actually asked for a solution to his problem the answer would be:

use parse_str($str, $result);

2
  • Actually not an answer to this question(?) but I like it +1
    – Lucas
    Commented May 7, 2014 at 20:20
  • This neither appears to answer the question or solve the underlying problem?! Perhaps you could expand a bit as it is somewhat confusing now. Commented Jul 11, 2014 at 14:51

You must log in to answer this question.

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