157

(Note: I deliberated for a long time if my question is the same as this one. In the end I felt it's different enough, but feel free to point out otherwise.)

OP asks a question, and posts a sizeable chunk of code. They've clearly been working hard on it, but now they've hit a roadblock and thus are turning to us for help. They've done their homework, they describe their problem and what they've been trying. By all accounts, their question should merit an answer.

The problem is, their code is fundamentally flawed. It's not that there's some little thing they neglected or misunderstood; it's wrong on a conceptual level, and that wrongness has manifested in so many small ways in the code. Basically, the most helpful answer I could give would be "Start over".

These kinds of questions make me feel trapped. Simply flagging the question as "too broad" or "off topic" feels unfair, since the question is specific and the OP has definitely put in the work. I could help the OP kludge together a solution that could get their code to run, but that feels even less helpful, since I'd be encouraging their really bad practices. And explaining everything that's wrong with the code would take inordinately long (not to mention it would probably be pretty crushing for the OP, even though it's for the best in the long run). It just feels like there's no good answer.

So, I was wondering: do others have strategies for how to handle a situation like this?

14
  • 91
    I've gotten mileage out of treating code as discardable. Nobody ever got upset (that I know of) when I commented "you'll have to throw this code away", along with hints on how to do it right. Strictly focusing on the code being wrong, not the programmer. Commented May 14, 2018 at 22:51
  • 8
    @HansPassant I'd like to see that as an answer. It addresses something that is not explicitly stated in the other answers here - that throwing code away, is part of the craft. Commented May 15, 2018 at 13:12
  • 9
    I see this a lot with poor security practices, when someone is asking 'How do I fix this login page/password hashing algorithm/etc. that I've written myself' and the correct answer is, in many cases, "don't"
    – JeffUK
    Commented May 15, 2018 at 14:19
  • 3
    @JeffUK Information Security SE has a canonical Why shouldn't we roll our own. Some of those question askers can be referred there. Commented May 15, 2018 at 14:33
  • If I run across a question that has multiple issues, I will make comments about those issues, and direct the user to what they are. If they fix those and update their question, I'll delete those comments and post more, until the question gets to a point that I feel that there is one specific issue that can be answered.
    – Taplar
    Commented May 15, 2018 at 16:42
  • 6
    The problem i have with some of the questions like that, is that the answer "You need to completely rethink your current solution. I can help you, and here's a good way to start... large post, with better coding/design strategies that mean the problem the OP has would never exist" are often ignored by the OP; the person you're trying to help. The reason being is that it's a lot of work to them. I find it so frustrating to see a user after a "quick fix", which they get too... but then a week later you see another question from them, due to the same poor design. :(
    – Thom A
    Commented May 15, 2018 at 17:08
  • @Larnu in the same area: OPs often accept answers that fixes their code, not the best answer that rewrites their code. Commented May 16, 2018 at 15:09
  • 2
    Remember that the "Too broad" close reason refers specifically to how long a comprehensive answer would be, not how long-winded or vague the question might be. You can ask a very specific question, but if the answer would take a 15 page paper to answer, then it's too broad.
    – TylerH
    Commented May 16, 2018 at 18:47
  • Enough with the hand-wringing: can't you just move on and do nothing?
    – Dexygen
    Commented May 16, 2018 at 20:17
  • From personal experience, the answer is yes. You should tell the OP he's doing something wrong and need to change his ways. Jon once told me that in this question, he made me change the way because mine was being done in a wrong way.
    – Paul Karam
    Commented May 17, 2018 at 8:58
  • @HansPassant I have.. I saw one guy barking up not only the wrong tree, but frankly he was even in the wrong forrest. Explaining that the code he had wasnt fixable, and even pointing him in the right direction. All i got was grief. Sometimes its hard to help someone who is convinced they are right and its only something minor.. when its not
    – BugFinder
    Commented May 17, 2018 at 9:39
  • "And explaining everything that's wrong with the code ... would probably be pretty crushing for the OP..." If it is, that's not your problem. Everybody makes mistakes, so you have to learn to take constructive criticism at some point in your life. On the contrary, I think most people would be very happy if you can explain exactly what they did wrong, even if it's a lot. Commented May 17, 2018 at 9:55
  • In that case, I provide general advices - eg. "refactor these blocks of codes in functions", "use an array and a loop to avoid duplicating the same code", etc.
    – laurent
    Commented May 17, 2018 at 11:38

8 Answers 8

75

I think this goes back to the heart of a challenge I had posed in another question.

Are we here to teach, or are we here to help?

I remain steadfast that our mission is to help, and we should help where and when we can, and to recognize situations in which we cannot help.

In turn...

[The OP has] done their homework, they describe their problem and what they've been trying. By all accounts, their question should merit an answer.

Their question would merit an answer if and only if it's something that is reasonably scoped. "Doing one's homework" is a common-sense prerequisite of anyone that asks a question here, especially given that it doesn't resonate well if you ask a question of someone and simply don't do your own due diligence beforehand.

There's a world of difference between someone posting everything they've done and their problem, and someone posting what they've done, what the result was, their desired state and their current state. Be sure you disambiguate those well.

The problem is, their code is fundamentally flawed. It's not that there's some little thing they neglected or misunderstood; it's wrong on a conceptual level, and that wrongness has manifested in so many small ways in the code. Basically, the most helpful answer I could give would be "Start over".

Guess what? That's an answer. It's helpful to say, "This is in a state that can't be improved on, you should look to start over with these things in mind...", since it gives the OP a chance to look at this with fresh eyes. What wouldn't be an answer would be to say, "Throw this code out, start over," and omit the extra guidance that the OP actually needs.

...Oh...

...And explaining everything that's wrong with the code would take inordinately long (not to mention it would probably be pretty crushing for the OP, even though it's for the best in the long run).

How much of "everything" do you need to explain to give the OP enough of a hint to go off of? If you have to start from fundamentals, abort and try again later. If there's one or two things missing, then make assumptions and fill in the blanks based on those assumptions.

I've never really run into scenarios in which I've encouraged someone to throw everything away, but in those contexts, if that's the best solution available, then that's what I'd recommend. No deliberation or hand wringing required.

11
  • 1
    "How much of "everything" do you need to explain to the OP" -- I guess part of the problem is my own obsession with being thorough in my answers. I just feel I don't want to leave the OP with nothing (or not enough) to go on after I tear down what they've been working on. Most of that might just be in my head though. Commented May 14, 2018 at 20:23
  • 23
    @MátéSafranka: That's a fair point. 8 years of experience as a CompSci tutor has taught me that giving something of a hint, even if it's not as thorough as you want it to be, is better than giving no hint at all. Those who demand your answers to be thorough instead of just being satisfied with the challenge of figuring it out are the ones you have to watch out for.
    – Makoto
    Commented May 14, 2018 at 20:24
  • 8
    When it comes to beginners, teaching and helping is almost always the same thing.
    – Lundin
    Commented May 16, 2018 at 9:49
  • 1
    Notice everything, focus on a couple of things, and mention one thing for the student to work on. Yes, helping and teaching are the same thing. Commented May 16, 2018 at 11:48
  • 1
    @Lundin: "For some people this is an intractable thing and inseparable from one another. For others, these two things are radically divergent and we have predetermined expectations of the asker." I suppose you can count me in the "radically divergent" camp, since teaching isn't quite the same as helping.
    – Makoto
    Commented May 16, 2018 at 14:54
  • 3
    @Lundin I disagree; you can help someone who is hungry by giving them a fish; you don't have to teach them to fish, and in some cases they don't even need to learn how to fish, they just need a fish to eat until they make it back home where they have all the equipment and know-how necessary to hunt for deer (for example).
    – TylerH
    Commented May 16, 2018 at 18:50
  • 3
    @TylerH Or multiple times more likely on a site like SO: they don't want to learn how to fish and if you give them one, they will come back for more of the same next time they are hungry. And then later they start a career as fisherman.
    – Lundin
    Commented May 17, 2018 at 6:25
  • When I ask something is because I don't know the answer. You providing an (acceptable) answer is providing knowledge I don't have. That's teaching. Commented May 17, 2018 at 9:24
  • "start over with these things in mind..." And putting those things to words in a way that a newbie can understand can be pretty tough. But omitting them also just makes for a rude response. And I see a lot of comments like that on VLQ questions. "RTFM" instead of "RTFM, because X, Y and Z". Commented May 17, 2018 at 10:12
  • @Lundin If someone comes back and asks for another fish and you recognize that, you can act appropriately (e.g. shifting from helping to teaching). But you can't assume all of that for each new user asking a question.
    – TylerH
    Commented May 17, 2018 at 13:18
  • @TylerH I've read at least ten questions just today, where the OP is asking some manner of XY problem. Someone who "helps" posts a fix to the conceptually incorrect solution Y, which still does not solve problem X. Someone who teaches posts a solution to problem X.
    – Lundin
    Commented May 17, 2018 at 13:30
25

I don't think closing accomplishes a lot here. The questions you have to watch out for tend to be "attractive nuisances", questions that lure in answerers but aren't actually answerable in any useful way.

...The sort of question you describe may be difficult or even impossible to answer well, but its saving grace is that it also looks difficult to answer. Unlike the "write me a Facebook" questions where the question seems trivial but the answer can fill a book, there's nothing trivial about this question - the asker has painted themselves into a corner and knows it, and so does everyone reading.

Of course, there's no hard line between these categories - it's entirely possible for someone to spend a month trying to do something, post copious details on their project and where they got stuck, and still ask for help doing something that's just impossibly broad... But even then, it's still less attractive than the one-liner "How do I do ALL THE THINGS?".

As Makoto notes, if you can answer - even just to point out in broad strokes where they went off the rails - then that's the most expedient way to handle these questions. The asker has something to work with, future readers have something they can learn from, and other answerers are free to either support or dispute (with their own answers) your assessment of the situation.

Otherwise... Just walk away. Leave it for someone else to evaluate. We have an entire site dedicated to careful analysis of working code, it's not unthinkable that someone might find it productive to do the same for unworkable code.

2
  • 3
    But if there is no useful artifact for anyone else to follow as a result, is anything of value created? I suppose if the "conceptual wrongness" is a common pattern, and not specific to one programmer's code? Commented May 14, 2018 at 21:44
  • 5
    IME that depends a lot on the answerer (if anyone dares try) and on how common a trap the asker has fallen into. Years ago I saved myself a bit of frustration learning from others' failures with double dispatch in C++; OTOH, there are almost certainly plenty of situations that are probably too specific/obscure to ever get hit again. A lot of these do end up unanswered and deleted automatically.
    – Shog9
    Commented May 14, 2018 at 21:53
6

I get where you guys are coming from. I may be trespassing on sacred ground here since I am very much new to R coding and SO (as a member), but here are a few ramblings from a newbie.

It is very intimidating and difficult to post a question on SO IMO - just naming the question/comment is hard enough and not everybody has the gift of "creative communication". Because of that, one can spend countless hours searching and perusing previous posts and solutions only to find that it is completely useless despite having a title / subject / tag that seems right up the alley.

A new R user, or even a seasoned veteran, may at times have blinders on to another way of doing things. I, for one, probably write inefficient code most of the time just because I'm coming from a background in Excel VBA, Python or what-have-you. Hence I have "natural blinders" just because I don't have the experience yet. For myself, I am lost without experienced guidance / feedback such as that found here on SO.

I'm sure I'm the only one who has these unique situations that aren't covered in the R books out there (I have probably 10 books / reference materials and most are worthless to help me with the "real world" problems I've come across....). Typically the only "answer" you can find to a problem in a book or another site is how to find the mean value of something. But unique problems are the real world; as it seems I have yet to need to find the mean of anything. SO is by far the best place for a person like myself to come for help because of the experience of the contributing members.

So in the end, without help from veterans such as SO's contributing members, I am left with days and weeks of fruitless work and very few options for a solution (since no one I work with knows what the hell R even is). SO is by far the best venue for people like me to hopefully get an answer. It is a tremendous shot of encouragement when one of you smart dudes points me in the right direction - whether it be to tell me I'm barking up the wrong tree, point me to a post that is useful or whatever else. You are much appreciated and needed for the betterment of the R community.

3
  • thanks, but note that if your code works but needs improvement, as Shog9 mentionned, there's codereview.stackexchange.com Commented May 16, 2018 at 12:05
  • Fyi, the RStudio forums might be a good fit for some of your questions (though first check out their FAQs to assess suitability, where to post, etc community.rstudio.com/faq )
    – Frank
    Commented May 16, 2018 at 12:44
  • Thanks for the suggestions - I will give them a go
    – JDC
    Commented May 16, 2018 at 14:22
6

In such cases, where it's fairly clear that the OP has made an effort to post a good question, and that they genuinely want to learn, I'm happy to try to help them.

Such OPs generally want to know what they're doing wrong, they don't just want a working alternative handed to them on a plate. However, it's usually not practical to go through all of their code and explain every mistake. In a sense, each different mistake could be a question in its own right, and in that sense, they've asked multiple questions in one, which makes it too broad, and of limited use to future readers. So we can't really afford to put their code under the microscope, we need to take a broader approach.

My strategy is to explain a couple of their key errors in detail, and possibly link them to relevant docs, or even an existing good SO answer, that will help to correct their misunderstandings. In some cases it may be useful to refer them to a tutorial.

And then I explain a better way to perform the task. I re-write the code from scratch, with plenty of comments where necessary to highlight how my logic differs from theirs, or to explain more obscure language features. I may re-use the OP's variable names to make it a little easier for them to follow my version, but I have no hesitation in improving their names when appropriate.

If it turns out that they want more detailed help about one or more of the individual mistakes in their code encourage them to ask a fresh question with a MCVE that focuses just on that mistake. Do not keep adding details to your answer in response to comments from the OP. That way lies madness, and chameleon questions. ;)

5

It depends. I recently posted an answer to a question much like the one you described, that already had an accepted answer that was focused on how to get the OP's wrong to to work with minimal change.

I only posted my answer because the OP had accepted what I think is a harmful answer (for the same reasons you wrote in your question), and downvoted the accepted answer.
I got into a "fight" with the poster of the accepted answer for downvoting his answer with what I think is a well articulated comment on why I downvoted, and I think I might have even suffered a few downvotes on my questions as a result (though it's not worth the trouble of reporting a suspect serial vote). At the end, the accepted answer got converted to a comment by a moderator, I've got 5 upvotes on my answer, but not accepted so I think either the OP didn't even bother to check it.

However, writing that answer took me about 10 minutes (which is not that long), and I had time that day.

I personally believe that if you are going to bother posting an answer to such questions, you better make it a good answer.
Keep in mind the answers you write on StackOverflow are not just to help the OP, but here to serve future readers that might encounter the same problem (In fact, that's the declared main purpose of StackOverflow - create a quality knowledge base of questions and answers).

1
  • OT but "At the end, the accepted answer got converted to a comment by a moderator" I didn't know it was possible.
    – Veve
    Commented Sep 25, 2018 at 21:57
3

I'm wondering if your sticking point is "sizeable chunk of code". "sizeable chunk of code"s are a "question smell". You shouldn't be apprehensive about closing a question just because the OP has posted a question that has a lot of code involved.

In fact, I'd say you should be more prone to closing it since is probably too broad. If the OP had posted a short 100 line MVCE that still demonstrated the issue, but also contained the fatal flaw, what would you do?

If the question is still on topic, I would probably try to answer in such a way as:

You can't do it this way because <reason>. The way that you are currently implementing will cause a <race condition | exception | unmaintainability | some other issue>. The main problem is here:

some code block commented with the issue

An alternative path to go down might be:

code that fits the spirit of the MVCE, but avoids the fundamental issue

If you look at this example, you will see that <reason why issue is avoided>

If it wasn't on topic, take the normal action steps:

  1. Comment and ask for clarification
  2. Try to get the OP to edit the post to be on topic
  3. Try to edit it yourself to get it on topic if possible
  4. Vote to close with appropriate reasoning
  5. Downvote (if appropriate)

Don't let a poster's effort be a reason to leave a post open. Effort isn't part of the vote to close tooltip, it's a part of the up/down vote tooltip. There have been some off-topic posts in the past that have been deserved of upvotes, but also deserved of close votes and vice versa.

1
  • 3
    Oh believe me, I'm extremely wary about questions that have hundreds of lines of code; those just reek of "I have no idea what I'm doing, here's everything I have please fix it". A "sizeable" chunk of code is around 100-ish lines, as you said. What I was trying to convey is that it's significantly more complex than just one or two functions. Commented May 15, 2018 at 17:08
3

The context of the problem is also helpful to understand. Other answers here have alluded to this.

If this is a production thingy that needs to be running yesterday in some form, duct-tape and bubble-gum solutions will be acceptable today, but also knowing there is a fundamental problem that needs to be addresses in v.2 is helpful and should be brought up. In fact, in any situation where the problem is intrinsic in some way to the code, where massive re-writes are the correct solution, the correct answer, even if it doesn't limit itself to this part, should include this pointing out of the deep flaws and the need for deep rethinking.

On the other hand, problems that are not in an emergency context should only include the necessary deep fixes, the pointing out of the inherent flaws.

The concept of the "Hero we need, not deserve" applies here too: the correct answer is the one that solves the problem, and a stop-gap is not a solution. It may be necessary in some circumstances today, but it is not a solution.

2

Yes, a downvote (and harsh comment like "argh wall of code ahead") would be daunting for this new user who tried hard, but in that current form it's not sure that the question (and potential answers) could benefit to future readers.

So to maintain quality on the site and help the OP at the same time, you could:

  • do as many constructive comments as you can on how to improve the code/design
  • vote to close as too broad/unclear/no MCVE
  • retract close vote if the question magically improves in the meanwhile

If someone answers first, well, good for the OP. If it gets closed first, maybe OP will edit using your (and others) comments and the question will be reopened: it then becomes a better/good question & gets good answers: win-win situation.

And if it's just a design problem, and the code works/runs, maybe the right move is to redirect them to CodeReview so they can get a lot of constructive comments, even refactored code as answers (they're not Stack Overflow dump & they have standards & rules like here, (runnable/testable code for instance), so it's not always possible).

You must log in to answer this question.

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