What should we do with flawed attempts to solve a problem, where the flaw is unrelated, both are common, and the question isn't explicit?
This is an extremely common issue I've been running into with new questions, but I was prompted again by considering this old question I just dupe-hammered: Python - Check if a number is a square
We have a canonical for checking whether integers are perfect squares, so given the title that's an obvious closure. However, OP's code is also a textbook exhibition of a common logical error, which is a) addressed by the top answer and b) has nothing to do with number theory.
Specifically, the problem is with trying to "search" a list of candidates to see whether any of them meets a condition (equivalently: whether all of them meet the opposite condition, and negating that result), using an explicit for
loop, and eagerly return
ing a value the first time through the loop, whether or not the condition is met. (Correct logic should only return
affirmatively if the condition is met, and then return
negatively only after the loop - the same idea applies, mutatis mutandis, for checking for "all" candidates meeting the condition).
This is a very common logical problem with code written by new programmers. It's the sort of thing that I would otherwise close as a typo, but I know that it really isn't a typo - people writing the code really do expect it to work this way because they are confused in the same, recognizable and MRE-able way. For another example, see the canonical Why does non-equality check of one variable against many values always return true?; many things are marked as typos ("obviously you meant and
rather than or
, right?") that really should be duplicates of this question. The classic Python FAQ Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those? is also something like this: a problem that's easy to understand and that experienced developers might consider a typo, but that large numbers of people get wrong in the same way.
We don't, to my knowledge, have a good canonical for the "search with a loop for anything meeting the condition" problem, although ironically we do have questions addressing ways to do the search without a loop (specifically, using the built-in any
and all
functions). I'm working on finding or writing something like that, so that the logical issue can be addressed first, and then reference can be made to more sophisticated approaches like any
/all
.
My current stance is:
In most cases, a question that asks "how do I do X (using a specific technique | "manually" or "explicitly" | without a particular standard library tool)?" is really a duplicate of the base "how do I do X?". The right place to put "alternative" approaches to X-ing is under that main canonical; if it's possible (and at all sensible) to do it in a specific way that isn't covered yet, rather than have a separate question for that specific way, the correct tool is for someone to place a bounty.
However, for some values of X, it will happen that many people try to use a specific technique Y, and commonly get it wrong in the same way Z. Sometimes this will be X-specific, but typically it just happens that the X problem is good bait for using Y, and the underlying question isn't really about X at all, but about Z. We should have good Z canonicals that have a classic flawed Y implementation (with the Z flaw) in the question, that explain why Z is wrong, and also show (or reference other questions) alternatives to Y - and all of this should be agnostic to X, if at all possible.
But this is where we encounter a problem. Constantly, new questions are asked that look like: "I tried to do X. Here is my code: Y [which exhibits the Z flaw]. Description of how it failed." The first question I linked at the top is a shining example of that (aside from being 5+ years old). Note in particular that there is no question mark anywhere.
These are perhaps better than average, even, but I don't know what to do here.
- Is this a duplicate of "how to do X"?
- Is this a duplicate of the debugging question for the Z flaw?
- Perhaps both?
- Does it "need more focus" because OP presumably wants both answered?
- Is it "unclear" because OP didn't say which question is being asked? If so, should we insist on questions explicitly containing a question (i.e. following QUASM)?
Aside from that, I have been meaning now for a long time to make the complaint that the insistence on code examples is actively making many questions worse. Many times someone will come to Stack Overflow with "How do I do X?" and no code. While it's true that many values of X are overly broad, "what have you tried?" is often a terrible response. The worst-case scenario (surprisingly common) is that OP has tried Y, and got it wrong in the Z way, and posts it, and now the above dilemma has been introduced to a question that didn't need it.
Is there anything else I'm missing here?