31

One of the most common errors that inexperienced programmers (and even some pros) make is using = when they mean == in a comparison

if (var = value) {
    // do something
}

The most recent example is Math.random() returns same value every time

I usually vote to close these as just a typo. But there are thousands of questions like this that could potentially be used as a duplicate, which seems like a more helpful way to close it (plus, I can use my close-hammer if the question has appropriate tags). But trying to find a good one is practically impossible. Does anyone have a link to a good one?

EDIT:

I didn't realize that closing as a typo means that the question will eventually be deleted, since it's not very useful for posterity, while closing as a duplicate keeps it around. In light of this, I think I'll go back to closing these as a typo. I realize that this is not really what the cause is (as pointed out by T.J. Crowder in comments), but as far as being useful for searchers it generally might as well be.

25
  • What would constitute a good answer here? Not much to say on the issue IMO. How about stackoverflow.com/q/14289635/73226 Commented Dec 9, 2015 at 23:07
  • I like @MartinSmith's suggestion, because it's specifically about if statements.
    – Barmar
    Commented Dec 9, 2015 at 23:09
  • This is...tricky. There are so many languages that this applies to, I think it would be confusing to have a language-specific canonical. Commented Dec 9, 2015 at 23:11
  • 2
    But maybe we need a language agnostic one as closing a JavaScript question as a dupe of a C one doesn't seem that appropriate. Commented Dec 9, 2015 at 23:11
  • I suppose I could write one an answer it myself if there isn't one already. Although the languages where this happens tend to have such similar syntax that I don't think the cross-tag duplicate would really be a problem.
    – Barmar
    Commented Dec 9, 2015 at 23:12
  • 2
    Or searching for yoda JavaScript finds stackoverflow.com/questions/18539552/… Commented Dec 9, 2015 at 23:13
  • Use google to find canonicals. If you can't find one, sure you won't, then just write your own. Keep it in your back pocket for the next couple of hundred questions about it. It just takes a couple of dozen SO users to do this and it won't be a problem anymore. Well, assuming they'll stick around for it, seems you want to. Commented Dec 9, 2015 at 23:36
  • Searching for answers would likely be the fastest way to find a good canonical. Use "is:answer" and the "score:" parameter along with a tag and a keyword or two.
    – jscs
    Commented Dec 9, 2015 at 23:52
  • Actually, these questions should just be closed as a "typo". If you can't figure out what "Invalid left hand side in assignment on line N" (which points to an assignment in a conditional statement) then there isn't much hope. There is no reason to keep all of these questions around by making them into signposts, there are enough of them around that aren't going to be automatically deleted because someone went and posted an answer pointing out the typo.
    – user4639281
    Commented Dec 10, 2015 at 2:38
  • 1
    @JoshCaswell What to search for? "yoda conditional" finds some, but how would you usefully search for = and ==, which appear in many more irrelevant questions and answers?
    – Barmar
    Commented Dec 10, 2015 at 2:47
  • 1
    Another common one is for (...); -- the ; after the header makes this a loop with an empty body.
    – Barmar
    Commented Dec 10, 2015 at 21:00
  • 2
    @Don'tPanic Probably at least 75% of questioners think the problem is different from what it actually is, I still usually close them as dups of questions with the same underlying cause. A comment can clarify how the original relates.
    – Barmar
    Commented Dec 10, 2015 at 23:57
  • 1
    @GlennTeitelbaum It's not an error if it's what you really wanted to do, like if ($row = $db->fetch()). But in all the cases where we would close it as a dup, it's because they really meant to test for equality instead of assignment. That's why the question should be worded in terms of if statements not working.
    – Barmar
    Commented Dec 11, 2015 at 16:22
  • 1
    Im still not getting why so many up??
    – Maduro
    Commented Dec 11, 2015 at 18:00
  • 6
    @ArielMaduro Probably because many people share my frustration with questions like these.
    – Barmar
    Commented Dec 11, 2015 at 18:05

7 Answers 7

9

Your quest is a worthy one.

My view is that this probably needs to be language-specific. Yoda conditions are largely useless in Java, to pick a random example, so a C++ answer to a Java question wouldn't make a lot of sense.

For my part, if you just pick a fairly pure one (per language, roughly), perhaps purify it with an edit, and start using it as a dupe target, I'll be behind you. :-) Sure, it may not be the earliest version, but that's not really the point, is it? The point is to have a question people with this issue can understand, and good answers to that question they can understand and use.

4
  • I don't program Java, so I don't understand why Yoda conditions wouldn't work there. Does its OO design really allow assigning to literals? But even if Yoda conditionals aren't appropriate, the simple solution of changing = to == should work for almost all conventional languages. I personally don't recommend Yoda conditionals (I think they look weird), at most I occasionally mention it as an approach some people like.
    – Barmar
    Commented Dec 10, 2015 at 23:54
  • @Barmar: FWIW, in Java, the expression being tested must be a boolean, there's no coercion like there is in C, C++, JavaScript, and various others. So for instance, if (a = 10) won't compile in Java. The only time a Yoda condition helps you in Java is when you're using a boolean variable (if (x = true) where x is declared boolean), and if you're using a boolean variable, you shouldn't be using == or != anyway except in very rare situations. But this is my point for why there may need to be more than one canonical answer. :-) Commented Dec 10, 2015 at 23:57
  • That probably means that the error doesn't happen much in Java in the first place. So the fact that a generic answer doesn't apply shouldn't be a problem. But a single answer could work for C/C++/PHP/Javascript and probably a few other languages, why would we need different ones for each of them?
    – Barmar
    Commented Dec 11, 2015 at 0:08
  • 1
    @Barmar: That's why I said "roughly." There are definitely languages that could share. Commented Dec 11, 2015 at 0:09
8

What if instead of making a canonical answer for it, we create a feature request to extend the dupe hammer power to typo questions? Surely someone who is trusted enough to close questions as duplicates in a particular tag would be able to also identify questions that are just typos accurately in that tag.

If we're trying to make a canonical question/answer pair so we can close questions as duplicates that should really be closed as typos because we can't close them fast enough as typos, maybe make closing them easier.

There's nothing subjective about seeing if (x = 5) and knowing without a shadow of a doubt that it's a typo. It seems like we're trying to work around the problem rather than solve it.

21
  • 20
    It's not a typo. This is a tpyo. Using = instead of == is a fundamental misunderstanding of the operators. Commented Dec 11, 2015 at 0:10
  • 1
    I've been thinking of suggesting a typo-hammer. But I'd still prefer to use dups for this type of problem, because the answer in the linked question provides a better explanation of the problem.
    – Barmar
    Commented Dec 11, 2015 at 0:10
  • 3
    @T.J.Crowder The typo close reason tends to be used as a catch-all for all minor errors like this.
    – Barmar
    Commented Dec 11, 2015 at 0:12
  • I think in addition to the point about whether it's actually a typo, there's something of a potential dynamic going on when having an ideal answer to redirect all such questions involving a confusion between = and == in that it might gain some kind of momentum in helping to dupe hammer these faster, and towards the same place.. maybe in an optimistic (maybe very optimistic) scenario, even help prevent some of these questions from being asked as a result of that momentum of all such questions having a redirection "root" in common.
    – user4842163
    Commented Dec 11, 2015 at 0:13
  • 2
    I don't think misunderstanding the operators is a minor error. Commented Dec 11, 2015 at 0:14
  • 2
    I'm sure it's possible for someone to misunderstand = isn't for use in a conditional in javascript, but... i think it is safe to assume 9 times out of 10 that it is just a typo. Do we have an example of someone actually answering this in a way that is useful, other than simply saying "= is for assigning, == is for comparing"? I can't think of a way of expanding that other than going further into explaining how to also use === and the differences, but... that probably already exists as == vs === and isn't really relevant to the problem.
    – Kevin B
    Commented Dec 11, 2015 at 0:15
  • @KevinB Very true, there's not much of an answer there let alone a question.. but it does seem preferable for even such a barebones question and answer to be revealed through a redirection rather than people jumping to provide the answer to the massively-duped question (issue?).
    – user4842163
    Commented Dec 11, 2015 at 0:17
  • 3
    @KevinB: "i think it is safe to assume 9 times out of 10 that it is just a typo" Not in my experience. Not remotely. People new to languages in the B syntax tree are not typing = instead of == by accident. They're doing it because all of their previous experience in life tells them that = is a comparison. Commented Dec 11, 2015 at 0:20
  • 1
    @T.J.Crowder I have a similar experience having had the unfortunate task of teaching CS 101 for a year. The students weren't making typos so much as misunderstanding the difference and how expressions actually worked. They needed a bit more hand-holding than merely a, "you made a typo", though maybe that level of guidance is out of this site's scope. A redirection to a commonly-cited source seems the most efficient attempt at this, however, if any attempt is made beyond trying to get such Qs deleted.
    – user4842163
    Commented Dec 11, 2015 at 0:22
  • 1
    Using = for comparison can most definitely be a typo. I started programming with Pascal, where = is the comparison operator, and after switching to C-like languages it was, by far, the most common typo I made.
    – Antti29
    Commented Dec 11, 2015 at 7:17
  • 2
    @T.J.Crowder I think we're quibbling over terminology. The point is that the questions of this type serve little purpose, and need to be closed. Whether the reason for the mistake is a typo or misunderstanding, the fix is trivial. And they'll never be found in searches because the description of the problem has no relationship to the actual problem. Closing them as dups helps the OP better.
    – Barmar
    Commented Dec 11, 2015 at 16:28
  • 3
    @Barmar: My only point wrt typo vs. not is the difference in how we deal with them. If we close as typo, then at best the OP gets some throw-away comment as explanation. If we close as dupe (e.g., implementing your canonical question request), they get a well-thought-out explanation. Commented Dec 11, 2015 at 16:40
  • I see your point. I've been using typo as the close reason in the past because I didn't have a good dup to point to, and it seemed most appropriate after someone posted an answer pointing out the error.
    – Barmar
    Commented Dec 11, 2015 at 16:57
  • 2
    The question that Barmar linked to uses = incorrectly 9 times. That's not a typo, it's fundamental misunderstanding of the syntax. I would say at least 50% of the posts I see like this contain multiple misuses of =. Commented Dec 11, 2015 at 20:59
  • 1
    This would be interesting, but how do you propose to allow the software to grant special privileges to a custom close reason? Commented Dec 12, 2015 at 0:17
3

I see a similar problem in tags like C and C++ where maybe 50,000+ questions boil down to just basic, "How do pointers and memory allocation (stack and heap) work?"

The trouble is that they're asked in 50,000 different contexts, with 50,000 different MCVEs, with questions as misleading as, "Why does my inverse kinematics solver segfault?"

Answer: "Because you're trying to access a dangling pointer to memory allocated on the stack which was freed. This is UB. Same issue and same answer as those 10,000 other questions with 10,000 different MCVEs. NEXT!"

The ideal scenario to me is to have one person just cut to the chase and ask, "How do pointers work?" or "What's the difference between operator= and operator==?"

That's a very broad question and it'd require an elaborate answer: a miniature booklet on a big section of a programming language with people competing to provide the clearest and most complete answer, but it'd be the ideal Q&A to link to which could potentially shut down tens to hundreds of thousands of basic troubleshooting questions all essentially asking the same thing like this as dupes, while linking to a comprehensive question with very elaborate answers -- a potentially great learning resource for such beginners who want to ask these types of questions over and over and over in different forms which boil down to such a simple misunderstanding of the language.

The unfortunate side is that a question like, "What's the difference between operator= and operator==?" would likely be considered too broad and shut down. But that leaves us with questions like, "Q: What's wrong with my particle simulator?" A: if (p.x = x) should be if (p.x == x) which really isn't addressing the heart of the issue. Such troubleshooting questions typically don't yield very informative answers either -- they don't explain why operator= and operator== are decidedly different, how to best avoid these mistakes, etc., since the educational value of the answers is diminished to a minimum by a very context-specific MCVE. To get such informative answers to redirect people to usually requires a more general question.

8
  • I think someone linked to a "What's the difference between =, ==, and ===" question in a comment, but then he deleted it after I expressed preference for a question about "if". And said question hasn't been shut down.
    – Barmar
    Commented Dec 10, 2015 at 17:10
  • @Barmar I've been thinking generally that the dupe hammer, used correctly, is the ultimate way to keep the site clean while educating the user who asked the dupe Q more than merely shutting down the question without a redirection. But it would be so helpful if we had that ideal general question always handy as a target to redirect. If there's already a very general Q about the difference between =/==/===, maybe even with an answer indicating the common human error of mistaking these operators in an if statement, it seems so ideal.
    – user4842163
    Commented Dec 10, 2015 at 17:26
  • @Barmar ... but one of my worries is that those ideal questions to redirect to are often far from canonical. They'd be very general, lazy questions -- but the kind of thing that could be asked and answered once, and then become an ideal redirection target. I'm missing a lot of those general questions in the tags I look in a lot.
    – user4842163
    Commented Dec 10, 2015 at 17:27
  • Well, now I can't find the question the comment linked to (searching for "===" in titles doesn't work), so I can't see how the answers are worded. I suspect it goes into detail about the difference between loose and strict equality, and just briefly mentions that = is for assignment.
    – Barmar
    Commented Dec 10, 2015 at 17:31
  • 1
    A simple "What is the difference between =/==" would probably be too broad, but would it be outside the scope of SO to ask "Who was the <insert expletive of choice here> guy who decided that a single equals for assignment was a good idea"? It would produce a fairly unambiguous, historical answer, and when these (typically) novice programmers' questions get closed, they get a humorous introduction to Stack Overflow.
    – TheHans255
    Commented Dec 10, 2015 at 23:23
  • @Compynerd255 That's an interesting idea to kind of get that central question out there that hits the spot for so many of these basic Qs. Maybe another angle might be like, "What are the advantages of separating =/==/=== from a language design perspective?" Or would that be too broad? I always find it tricky to try to design the perfect question to direct beginners towards without making it this broad.
    – user4842163
    Commented Dec 10, 2015 at 23:36
  • @Compynerd255 "Why do humans make errors confusing operator= for operator==? "What is recursion? [marked as duplicate of itself]" :-D
    – user4842163
    Commented Dec 10, 2015 at 23:37
  • 1
    @Ike That second one is actually perfect - the answer to that proceeds to highlight what is actually a really cool feature of languages that allow this to compile - the fact that numbers evaluate to truth conditionals. In quite a few cases, mainly canonical optimization cases such as those found in K&R, the "=" behavior is exactly what you want.
    – TheHans255
    Commented Dec 10, 2015 at 23:42
1

I don't see much discussion here of linters except one comment. 99% of the maybe-a-typo-maybe-a-syntax-error problems could be discovered via a simple lint. Certainly = vs. == would be. Probably 50% or more of them could be solved by opening the console and looking at the errors. Whatever problems remained of the simple-logical-error-brain-fart variety could be easily resolved by some trivial debugging.

I've added comments to dozens if not hundreds of posts reminding people they could lint, or look at the console, or debug. So have others. No small number respond saying they hadn't heard of those things. It's getting really old.

The bottom line is that the vast majority of low-rep users asking lame questions apparently have never bothered to educate themselves about how to open the console or simple debugging approaches and tools, much less use a linter.

I'm tempted to write a canonical question/answer titled "My program doesn't work, and I think it might be a typo; how can I find it?", and then close questions as a duplicate of that, but that's perilously close to an abuse of the duplicate reason.

So here is the 1,291th proposal fixing or adding close reasons:

Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. In addition, only questions where the code has already been verified by linting tools for the language in question, syntax errors already found and resolved, and appropriate debugging techniques used to try to find the problem are allowed.

Or if we want to expand the "typo" reason instead:

This question was caused by a problem that can no longer be reproduced or a simple typographical, syntax, or logical error, that could have easily been identified using a linting tool for the language in question, viewing and/or properly interpreting error output from compilers, interpreters, and run-time environments, or using basic debugging techniques. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting.

(The word "simple" here is meant to modify all three types of errors: typographical, syntax, and logical.)

Or something like that, you get the idea.

So why do suggestions like this, which are definitely nothing new, get only limited traction? It seems to be that there's a built-in conservative (i.e., do nothing) bias. For any given proposal, up pop folks constructing elaborate scenarios in which some poor, well-meaning noobie's question is cruelly squashed by some nefarious, malicious high-rep user. Most of these counterarguments boil down to a lack of trust in people who in some cases have spent hundreds, or more likely thousands, of hours on the site over a period of years. But this is not the criminal justice system: our criteria is not to prefer a hundred bad guys go free rather than a single good guy being falsely imprisoned. The whole point is to not let the bad guys--I am referring to the purveyors of mass pollution on the site in the form of low-quality questions and answers--go free, even if it does mean that one "innocent" is affected. We already know some of the tools to use to mitigate these kinds of risks, such as time-limited or tag-limited experiments. Let's use them more aggressively in pursuit of creatively addressing these issues.

2
  • PHP, Python, and Javascript don't produce any warnings for the = vs == error. Some C compilers do, some don't, or you have to enable extra warning options. Users who post these questions are almost all too inexperienced to know about third-party lint tools for these languages. They mostly don't even know how to use the existing debuggers. So while your idea is well intentioned, it's not really practical.
    – Barmar
    Commented Jun 4, 2016 at 8:18
  • 3
    Your comment is essentially equivalent to saying that on a Q&A site about toasters, we would have no close reason for questions about the toaster not working when the problem was that the user didn't check to see if it was plugged in, because some users are almost too inexperienced to know that's required. I don't care if people know about debuggers or linters; I just don't want those people posting here and when they do I'd like to close their questions more quickly, and possibly even help some small percentage of them by alerting them to these kinds of tools.
    – user663031
    Commented Jun 4, 2016 at 8:27
0

IMHO, there is only marginal value for questions of this type. They are, by definition, typographical errors and as such are clearly qualified as off-topic. That's why there is a specific off-topic close vote reason for typographical errors.


I understand the appeal of having a canonical question to use to mark as duplicate, but ultimately these types of questions are not useful. And while I agree = vs == is a common mistake to make (in C/C++ for example…some other languages have eliminated this particular kind of error), there are lots of other common typographical errors one can make. These are best left to the compiler (when they are syntax errors) and code analysis tools (when they are valid syntax but incorrect code) to detect. I know for a fact that Visual Studio's "PREfast" code analysis will catch this kind of mistake, and I'll bet other tools do as well.

I really don't relish the thought of seeing dozens or hundreds of new, useless questions and answers added, just so we have a way to close typographical-error questions more quickly. Even if we did have such a way, it would be a drop in the bucket compared to all the other useless questions that are posted to Stack Overflow every day. What we really need is a more general-purpose mechanism to combat the tendency of users to answer obvious duplicates instead of actually finding the real duplicate, and to fail to close questions that are off-topic.


All it takes is five people to close a question like the one you're asking about. We should try to find ways to make the existing process work more efficiently, rather than encourage Q&A's to be written for what is a blatantly off-topic question.

1
  • 2
    Speaking from C and C++, all of MSVC, GCC, and Clang will gladly warn about mixing up = and ==. That's even better than a code analysis tool, boiling the questions down to using a decent compiler and maybe enabling some basic warnings.
    – chris
    Commented Dec 10, 2015 at 23:41
-2

No, don't do that. This is a terrible idea.

Questions posted accidentally assigning instead of comparing always contain the vaguest of titles, and the most convoluted example of why something wasn't working.

What we do not need in google results are "jQWURY IS BROKE" and then a link to a canonical answer about how to use == instead of =.

A million times no.

18
  • Isn't that the point? Answering most of these questions does no good, because no one will ever find them when they search for the problem they think they have, because the title and question bear no relation to the actual problem. So we should close all of these questions as a duplicate of one that has a good title and question.
    – Barmar
    Commented Dec 11, 2015 at 19:16
  • @Barmar - The link from google will go to the good title and question, but the result in google will show "jquery is broken" or "Where condition causes random number generator to produce negative numbers" or something else which has nothing to do with = versus ==.
    – Travis J
    Commented Dec 11, 2015 at 19:20
  • 2
    Basically, the canonical certainly helps the op, but is unlikely to help future visitors and will result in the question staying around because it's a dupe closure and not a typo/unlikely to help future visitors closure.
    – Kevin B
    Commented Dec 11, 2015 at 19:22
  • I agree @KevinB, it would help the OP. But it would leave behind a mess and that would just accumulate over time.
    – Travis J
    Commented Dec 11, 2015 at 19:23
  • 2
    I don't understand the google problem. In my experience, the title shown in google results is always that of the question it links to, not some other page that links to it.
    – Barmar
    Commented Dec 11, 2015 at 19:23
  • I didn't know that typo questions get deleted automatically.
    – Barmar
    Commented Dec 11, 2015 at 19:24
  • 1
    not instantly, they get deleted after a period of time if they have no upvoted answers, just like other closed questions not marked as dupes. Reference: meta.stackexchange.com/questions/5221/… This works best when they are closed quickly without having an answer posted.
    – Kevin B
    Commented Dec 11, 2015 at 19:24
  • @Barmar "In my experience, the title shown in google results is always that of the question it links to, not some other page that links to it." I think Travis is referring to the fact that anonymous users are automatically redirected to the dupe target...they never even get to see the original question. Commented Dec 11, 2015 at 21:09
  • Which should be OK, because the original question is probably poorly worded or titled, so it's irrelevant. The dup is the one that expresses the problem well.
    – Barmar
    Commented Dec 11, 2015 at 21:10
  • 1
    My issue with this problem is searchers are unlikely to be searching with terms that would result in reaching the dupe target or even any of its duplicates. while that's not a reason to change teh way we close it, it's an indicator that the question itself that we are closing as a dupe doesn't have a proper MCVE. the problem they describe in the question is completely unrelated to the actual problem they have, making the dupe useless. Why close it as a dupe and keep it around? it's only going to be useful up until the op goes to the dupe target.
    – Kevin B
    Commented Dec 11, 2015 at 21:12
  • @Barmar I think his point is that people searching for "jquery is broken" are 99.9% of the time not going to be helped by a post saying "you used = when you should be using ==". Of course, the original post would have been equally useless for them, as would most such questions with crappy, vague titles, so I don't see how closing as a dupe makes things worse. Commented Dec 11, 2015 at 21:16
  • 2
    closing it as a dupe leaves it on the site for years to come, where as closing it at a typo could have it deleted in as little as 9 days later. but you could of course argue what harm does it do to stay around, no one's gonna find it anyway. I prefer for useless crap to be deleted asap, but i can't come up with an argument for why that matters.
    – Kevin B
    Commented Dec 11, 2015 at 21:17
  • 1
    As I said above, I didn't know about the automatic deletion of typo questions. That makes closing as typo more useful for the community in general.
    – Barmar
    Commented Dec 11, 2015 at 21:32
  • 1
    @Barmar It's not just typo questions. Any closed question that is not marked as a duplicate and has a negative score, no accepted answer, no positive-score answers, and a couple of other criteria is deleted automatically after 9 days. Commented Dec 11, 2015 at 21:45
  • 1
    @Barmar That was just for your general knowledge :) The roomba comes in handy for all kinds of crappy questions. Commented Dec 11, 2015 at 21:51
-4

If you post a canonical question/answer, it will likely be closed as a dup

There would need to be support for canonical questions and likely a required rep to post one

imagine 1000 questions asking about = vs ==

some of them are market as dups and point to other similar questions

if you post a WIKI stype q/a as the canonical, today it will likely be marked as a dup of one of the possible 1000s

if instead of mark as dup, there was a create canonical wiki then that post would be redirected to the canonical, and all future similar questions could be linked to the canonical and possibly the prior 1000 could be redirected as well (you redirect one in the dup web, and all get redirected)

You would also need to be able to merge canonical dups so one becomes a dup and links the "better" wiki

This unfortunately is a lot of work, and prone to judgement calls on the "best" wiki

2
  • 3
    A dupe of what?
    – Robert Harvey Mod
    Commented Dec 11, 2015 at 16:18
  • 1
    The purpose of posting a quality canonical is that while yes, it is a dupe of thousands of other questions that share the same problem, it's far better written such that it can be found by those trying to close this specific question so that we can more easily guide the OP (and the community) to an answer. It still likely won't result in less of these questions, it will just become easier to close them while still helping the OP.
    – Kevin B
    Commented Dec 11, 2015 at 16:43

You must log in to answer this question.

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