3 ms·
"Yoda Conditions", "Pokémon Exception Handling" and other programming classics
- heyitsnick 15y agoIt's a shame it didn't mention the _reason_ for Yoda Conditions: as a protection against accidentally using the assignment operator rather than a check for equality. Accidentally writing if (foo=4) { } will not raise an exception, whereas if (4=foo) {} does, thus alerting you of your typo rather than allowing a rather nasty bug to persist.
- cperciva 15y agoOf course, any compiler written in the past decade should print a warning on if (foo = 4), so this reason doesn't apply much any more.
- andos 15y agoI used to find lots of yoda conditions in Java. People use it to test a string variable against a constant value and save some null checks.
- dhotson 15y agoI use them when doing an assignment and equality check in the same if statement. I find that putting the equality check first helps readability and makes the intent clearer. eg. if (null == (data = cache.get('blah'))) // cache miss else // cache hit
- ekidd 15y agoThis post is copied from a famous StackOverflow thread, including the pictures: http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined http://stackoverflow.com/questions/2349378/new-programming-j...