Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • 4
    The line between modern/legacy languages isn't all that clear. For example in C (a legacy language?), you can have integers in if statement (anything not a 0 is "true"), so the = vs. == bug is a real concern. But even in Java, if you have booleans, both if(a=false) and if(a==false) are legal, but the result is different! (And at least my compiler doesn't warn about this case - why would it, because either case is perfectly correct code.) IMO that's a good reason to use if(""==$variable) exclusively. Commented Sep 18, 2014 at 6:36
  • 1
    @JoonasPulakka Decent C & C++ compilers warn about assignments in if conditions, precisely because it's a common source of error. You normally silence the warning by parenthesising the assignment - it's a way to signal to the compiler "yes, I know what I'm doing." Commented Sep 18, 2014 at 7:00
  • @Angew Funny how you seem to suggest that the Java compiler is not 'decent' :) Commented Sep 18, 2014 at 8:35
  • 1
    @marczellm In some way, yes. I feel that "perfectly valid, but most probably erroneous" code warrants a warning, if the compiler also provides a simple way of overriding it. Commented Sep 18, 2014 at 8:38