17

I am wondering how to deal with similar questions but the OP asks for the opposite result.

For example, let's say there is an old question asking "How to check if a word is not included in a string?", and the posted answer would be if(!sentence.includes(word)){}

Now there is a new question asking "How to check if a word is included in a string ?". The answer would be if(sentence.includes(word)){}.

I consider that the most important part of the answer is the use of includes().

Should I flag the new question as a duplicate or should I quote the old answer and make a new one with the little modifications?

1
  • 4
    The solutions to opposite problems can be very different sometimes. I don't think the concept of closing a question as a duplicate of something else when they are asking opposite problems is a good precedence. More likely, however, is there is a question that is simple enough to have very similar answers for opposite (like in this example, of adding an !) there are likely duplicates for both scenarios already.
    – Thom A
    Jul 12, 2022 at 15:09

3 Answers 3

10

This is a judgement call that should be made for each question. Within the narrow scope of your example, flag the question as a duplicate and post a comment that the duplicate answers the question but in an opposite way from the OP's question (worded better, of course).

Comments:


Does this answer your question? How to check if a word is not included in a string? — Cédric
(added by your duplicate close-vote)


The duplicate asks basically the same question, except if the word is included. Your question simply needs to invert the test. — Cédric
(custom-written comment to clarify how the dupe answers the OP's question)


You want your additional comment to hint that the duplicate does provide an answer, but you need to look at the answer from a different angle.

I would caution against applying this same logic to all questions that seem to ask the opposite of something. The term "opposite" is very general, however if the subject is about a boolean operator which by its very nature implies only two outcomes (and therefore only two code paths) then this would work. Be careful though, if the question has an if followed by an else if. The control flow of the application might be different than an if-else construct.

Ultimately you will need to read both questions and answers and make a judgement yourself.

If doubtful in any way, consider posting an answer to the question, and reference the potential duplicate. Make sure it is a complete answer that stands on its own without the duplicate, in case the duplicate gets deleted in the future.

You want to test if the sentence does not contain the word:

if (!sentence.includes(word))
//  ^

This is just inverting the logic from testing if the sentence contains the word. See How to check if a word is not included in a string? for a similar (if not duplicate) question.

1
  • 2
    And the corollary is that if you don't feel confident to make the judgement call ... or couldn't be bothered to read the related questions so that you can make a sound judgement ... then just leave it / them alone. Leave it for someone else with more knowledge and/or time.
    – Stephen C
    Jul 15, 2022 at 6:09
8

For something that common, chances are there is a dupe for both. So if there is an exact dupe, use it. TBH, I feel it is a bit of a judgment call, honestly. But that is of course subjective. Here's what I tend to go by (this is my opinion).

In your specific example, I'd flag as a dupe of this question (if the OP was using Python). The question is asking for a substring method, and one answer explains the in keyword ("hello" in "hello world") while another demonstrates it with a not (not is the Python version of ! ("hi" not in "hello world"). So sometimes the dupe might still have an answer that answers it, even if the first one doesn't! So that may work.

If there isn't an answer that explains exactly what they're asking, I'd be more wary about dupe flagging. Imagine, for example, someone posts a question saying "In Java, how can I see if the file ~/hello.txt exists so I can create it if it doesn't already exist"

You might be tempted to have it be a dupe of this question. But no, it shouldn't be! Because if the OP then might write code like this:

File f = new File(filePathString);
if(!f.exists() && !f.isDirectory()) { 
    // create the file
}

Unfortunately, that is wrong, as if some other program creates it after the if but before the Java code makes the file, the OP will have an issue. Instead, they should just try to create it and catch the exception if it exists.

Ideally, there is an actual dupe. But if it would really be as simple as saying "not something", I'd mark as a dupe. But please don't do that unless you are sure there aren't any hidden pitfalls with doing that.

5

The difference in the hypothetical scenario is trivial. You can change it either way and the answer materially is the same: the selection criteria changes, but the method to obtain the results is identical. Those are duplicates: they fundamentally ask the same thing. The one that should be closed as duplicate depends on the quality of the questions and the answers. Also, feel free to modify the title to surface the underlying problem: selecting sentences based on words.

You must log in to answer this question.

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