Re:How to Score a Google Onsite Interview
by latch on Saturday April 30, 2011 @11:15PM
(#2502211)
I don't know the difference between (“x” == x) vs. (x == “x”), and googling isn't helpful (hah!). Anyone?
[ Parent
| on HN ]
"CONST == variable" is called a 'Yoda condition'.
More information on this can be found at 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.
Furthermore, for many year now the compilers have been able to warn you if you use = instead of ==.
I 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.
Agreed, 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.
Re:How to Score a Google Onsite Interview
by epochwolf on Saturday April 30, 2011 @11:58PM
(#2502299)
Look 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 thereRight..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.
(x = "x") vs ("x" = x)
see http://stackoverflow.com/questions/214031/is-constant-compar...