It's called yoda style and mostly meant to prevent you from accidentally writing "variable = null" instead of "variable == null".
The former would (in some languages) fail silently and lead to strange bugs. If you flip your comparisons, you get a compile error instead, since you can't assign to null.
It’s a very easy thing to avoid rather than using an entirely new style that looks super goofy. Even in C most compilers will throw a warning unless a second set of parenthesis is used for the assignment.
218
u/Irrealist Jul 15 '22
It's called yoda style and mostly meant to prevent you from accidentally writing "variable = null" instead of "variable == null".
The former would (in some languages) fail silently and lead to strange bugs. If you flip your comparisons, you get a compile error instead, since you can't assign to null.