4 ms·
I don't know the difference between (“x” == x) vs. (x == “x”), and googling isn't helpful (hah!). Anyone?
by latch 16y ago
I don't know the difference between (“x” == x) vs. (x == “x”), and googling isn't helpful (hah!). Anyone?
- elq 16y ago(x = "x") vs ("x" = x) see http://stackoverflow.com/questions/214031/is-constant-comparison-variable-personal-taste-or-is-there-an-objective-reason-f http://stackoverflow.com/questions/214031/is-constant-compar...
- deleted 16y ago[deleted]
- epochwolf 16y agoLook at what happens if you miss a = (“x” = x) //Throws a compile error vs (x = “x”) //x is set to "x" Edit: sorry about the quadruple submit, don't know what happened there
- latch 16y agoRight..not a problem in Java/C# and no compiler benefit either way in a dynamic language. The "x".equals(x) might be slightly more relevant (no fear of x being null). Cheers.
- deleted 16y ago[deleted]
- deleted 16y ago[deleted]
- deleted 16y ago[deleted]
- Jach 16y agoI noted that ("x" == x) avoids the problem of (x = "x") that others mentioned, but really, any sane compiler will warn you about the second. (GCC does, mxmlc does, Python flat out disallows the syntax.) The way to shutup the warning is to say ( (x = "x") ) if you really mean it. I was more worried that the language in question was C++, so you have to worry about conversion constructors, conversion operators, operator==, etc.
- allenc 16y agoAgreed, although some laxer languages - say, Javascript - will let you do it all the same. Like I said, it's not necessarily a rule to follow, but whenever I come across people who would do things slightly off (from common coding practices anyway) I notice the difference, and if it's something I recognize I put a little mark there in case they're really, really close. I come from a C++ background, so stuff like i++ and ++i really do make a diff.
- Luyt 16y ago"CONST == variable" is called a 'Yoda condition'. More information on this can be found at http://united-coders.com/christian-harms/what-are-yoda-conditions http://united-coders.com/christian-harms/what-are-yoda-condi... , which describes its uses and advantages. I tried to use them a while, but not anymore, because I find them unnatural; like reading backwards. In my opinion, it's more logical to first introduce what is being compared instead of some value which will be compared against. This is especially true if you revisit the code after a few months.
- pfedor 16y agoFurthermore, for many year now the compilers have been able to warn you if you use = instead of ==.