Re:How to reduce the cognitive load of your code
by kilink on Tuesday March 29, 2016 @02:13PM
(#11383554)
Java if statements only allow boolean expressions, so the thing that the Yoda condition is supposed to protect against isn't even possible. I think this is precisely what the author is talking about, as this style is a totally unnecessary holdover from C.
Re:How to reduce the cognitive load of your code
by vitd on Tuesday March 29, 2016 @01:33PM
(#11383163)
For me the _habit_ of using (constant == mylar) is way harder to get used to than using "=" vs "==", which the compiler will warn me about if I mess it up.
Re:How to reduce the cognitive load of your code
by MereInterest on Tuesday March 29, 2016 @03:41PM
(#11384279)
I would disagree that they are hard to catch mechanically. GCC will warn about it with -Wparentheses, which is included in -Wall.
I'd rather write it that way, and then later change it in code review to `(myvar == constant)`, than risk writing it as `(myvar = constant)` and have it sneak through. Granted unit tests might catch this, but perhaps the error is in the unit test. ;)