-2

I occasionally see questions about mistakes caused by incorrectly assuming that (x == a || b || c) means (x == a || x == b || x == c) but cannot find an appropriate canonical for the language the question is in.

Some canonical questions exist but only for specific languages:

Why is there a language-agnostic canonical for a very similar issue but not for this one? What should I do if I find a question that is caused by that mistake but no language-specific canonical exists?

2
  • 3
    It's good to generalize, but it's also possible to overgeneralize, as would be the case here if you take crucial language specifics out of it. It's appropriate to solve this in O(n) where n is the number of programming languages that exist, as much as that may irk programmers (in practice, n is a small number). The "language agnostic" canonical would quickly devolve into dozens of language-specific answers starting with "in Ruby, ...", "In Python, ...", etc, and become chaotic.
    – ggorlen
    Commented Mar 26 at 4:07
  • 2
    The non-equality question addresses a logical mistake, and logic is language-agnostic. The x == a || b || c mistake is a syntactical mistake, and syntaxes are language-specific.
    – khelwood
    Commented Mar 26 at 10:04

4 Answers 4

11

I mostly agree with Kevin's answer post. For language-specific question posts, leave it at having language-specific canonicals. Every language is its own thing. It has its own grammar and semantics, expression evaluation order (whether language-defined, or implementation-defined), boolean coercion / automatic type conversion (or lack thereof), its own wider feature set (providing alternatives), and its own conventions and idioms.

If you make a language-agnostic Q&A for the purpose of duplicate-closure, you lose out on saying "this doesn't work because this language works like <this>. take your pick from <alternatives>, given <tradeoffs>", which is a whole lot of useful content.

Sure, this may be a common mistake, and it might look messy to see a lot of language-specific Q&A about the generalized point of confusion, but as I already said, each language is different. There's no law that says I can't create a language where this is syntactically legal, meaningful, and does what these askers probably expect. And I could do it in an infinite number of ways with syntactic variations.

Why is there a language-agnostic canonical for a very similar issue but not for this one?

Why does non-equality check of one variable against many values always return true? is a boolean logic and expression evaluation question, so I think it's just fine. I didn't initially realize due to the large amount of examples with code in specific programming languages (I'm a bit surprised there's no mathematical notation in the Q&A (at the time of this writing)). I don't see an issue with it being used as a duplicate target as long as the question being closed is about a programming language expression that follows compatible rules for evaluating boolean expressions (which is a pretty vapid statement: "close as a duplicate if it's appropriate to close as a duplicate"). Compare to the types of question you're linking to here, which are programming-language-specific.

What should I do if I find a question that is caused by that mistake but no language-specific canonical exists?

Let that one become the canonical. See if there's any way you can edit it to improve its clarity and make it a better minimal reproducible example. If you don't have the subject-matter expertise, leave it to someone who does.


The part where I possibly diverge from Kevin is that I don't think a language-agnostic question is necessarily not useful (as long as it's not used as a duplicate target). There are specific types of language-agnostic questions for things like this that I think are useful to have. Ex. Why do many languages not support nested block comments?, which I think is a good design+history question.

0
14

Given each language has its own (but sometimes similarly themed) "better" solutions to this specific problem, or even differences in how common the problem is or what thought processes lead to them, it shouldn't be language-agnostic.

Language-agnostic canonicals also fail to return in tagged searches, leading to these being easily missed when someone searches for them on-site.

7
  • The if (x != a || x != b ...) one about a different issue than if (x == a || b || c ...). One is about x being not a and not b at least one being true, and the other is about comparing the boolean x == a to b instead of actually comparing x to a and b.
    – CPlus
    Commented Mar 25 at 20:11
  • The other is about a failed attempt to do the former. It's a great example of where forcing users to show an attempt at solving a given problem can lead to a worse experience.
    – Kevin B
    Commented Mar 25 at 20:12
  • Would you close a if (x == a || b || c ...) question as a dupe of the if (x != a || x != b ...) canonical?
    – CPlus
    Commented Mar 25 at 20:14
  • If i could find it, yes
    – Kevin B
    Commented Mar 25 at 20:16
  • 2
    As a Python expert with extensive experience explaining these specific topics to beginners, I can assure you that they are completely different. The conceptual problems underlying them are unrelated. The current topic is about the fact that English doesn't accurately express certain logical propositions. The other one relates to a well-defined logical proposition not giving the expected result. So one is about expressing logic in terms a computer understands; the other is about the logic itself. Commented Mar 25 at 20:38
  • 2
    I can't speak for the issues beginners have with python, given i'm not even a beginner in python, much less an expert, but from my area with javascript there's no question every case of someone attempting x == 1 || 2 || 3 is looking for an efficient way of doing x == 1 || x == 2 || .... This is why i think we shouldn't have language agnostic canonicals in general. (they also fail to come up when searching using tags)
    – Kevin B
    Commented Mar 25 at 20:45
  • 1
    "but from my area with javascript there's no question every case of someone attempting x == 1 || 2 || 3 is looking for an efficient way of doing x == 1 || x == 2 || ..." - yes, and that's the same as in Python. The point is that the other canonical is about why x != 1 || x != 2 doesn't do the same thing as !(x == 1 || x == 2), and that has a fundamentally different cause (DeMorgan's laws of logic - not a failure to understand that the x == part isn't "distributive"). My point is that one question is definitely not about a "failed attempt to do the other". Commented Mar 25 at 21:18
11

The Python canonical has over 600 "linked" questions. The other two you cite have about 50 between them.

The problem is so specific to new Python developers (most likely because the ability to write natural-language or instead of a special operator || encourages thinking in illogical English terms) that it doesn't seem worthwhile to address the topic in a language-agnostic way.

1

For , the first place to have a peek:

  • Click the tag then "learn more" to enter the tag wiki.
  • Scroll down to the FAQ section
  • Below "Operators, precedence and order of evaluation" there is no exact duplicate, but one close enough, about a similar misconception and with the same answer:

Chaining multiple greater than/less than operators (common FAQ where beginners assume that "interval syntax" like 0 < x < 10 is valid C)

is identical here so canonical C dupes may be used to close C++ questions too in this specific case.

The duplicate you found for C++ is much more suitable for the specific case of &&/|| operators though. If you think it is a good duplicate, feel free to add it to the list in the tag wiki. Ideally with a little text explaining what it is for. Maybe:

In this case, probably not, since existing answers have a heavy C++ emphasis so it's not a good canonical dupe for C (but likely for C++).

Other considerations when adding a "canonical dupe" to the tag wiki:

  • Visit the post and tidy up the question if needed. Remove stuff like "...in C++" from the title.
  • Clean up tags. Crap like needs to go and maybe would be more suitable.
  • For C++ specifically, maybe consider adding the (not a fan, personally).
  • In case of high activity questions, protect it (15k+ rep).
  • Up-vote the question and all good answers. If you decided it's a canonical dupe then surely it's a good question, yeah?
  • Optionally, try to herd together some veteran / users in a chat like the SOCVR or C++ lounge and with consensus of several such users, delete all low quality crap answers that are just comments or repeating already posted answers. With luck you might even find some mods with domain knowledge willing to help out.
1
  • nittery: c++ has operator overloads, and argument evaluation order is unspecified. if you have impure functions, blah blah blah.
    – starball
    Commented Mar 30 at 20:05

You must log in to answer this question.

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