Re:Don't use the greater than sign in programming
by pj_mukh on Monday February 22, 2016 @09:59AM
(#11151170)
Thank god this is becoming common knowledge. I'm gonna be extra peeved if a programmer tells me she used a certain conditional because it was closer to the number line.
http://s45.radikal.ru/i108/0902/0f/000d1c614e24.gif
Re:Don't use the greater than sign in programming
by aeharding on Monday February 22, 2016 @10:25AM
(#11151384)
Also known as yoda conditions [1]. Some linters have options to enforce non-usage as a best practice [2].
Re:Don't use the greater than sign in programming
by TheAndruu on Monday February 22, 2016 @11:31AM
(#11151988)
Nice. I was aware of the style, but not the name for it.
Also liked the first line of the criticisms: `Critics of Yoda conditions see the lack of readability as a disadvantage that outweighs the benefits described above`
To that end, use a greater-than sign if reading the code makes more sense that way.
E.g. if the method name is `findAllUsersOverAge18()` then it'd be more sensible to have a check such as: `if (userAge > 18)` than to have a check for `if (18 < userAge)`
Both statements are technically correct, but the one makes a huge difference in terms of reading it and understanding it, without a Q/A: "Why is this conditional turned around?" "Well, Steve is opposed to ever using greater-than signs so we have to invert all our logic".
I also can't help but pointing out- except for certain conventions such as loop iterator variables, it's a much worse programming practice to use non-descriptive, single variable names, such as `x`.