A “Yoda Comparison” it is called when put the constant on the left you do. Back to normal english: the idiom of “Yoda Comparisons” comes from C, where this was a classic bug
if (foo = 123) { ... }
Thus assigning foo to 123 and making the test, all because of a single missing =
sign. On the other hand, this Yoda comparison won’t even compile because it’s trying to assign to a constant.
if (123 = foo)
This exact sort of bug even landed in the Linux kernel once, but it was if (euid = 0)
, which set the effective userid to 0, which is root. Yowza.
Yoda comparisons are still used in a lot of C codebases, but they’re falling out of style pretty much everywhere else because they’re annoying to read, and any decent static analysis tool will detect assignment used as an expression anyway. The idiom comes before such tools were widely available or commonly used.
Of course what’s funny about the example in the post title is that neither side of the comparison is an lvalue, so it’d never compile as an assignment. It just looks like someone got their brain wired into Yoda comparisons and uses them in every case.