4 ms·
It's better to write your code so that it's easy for a later person to understand it while reading. To that end, use a greater-than sign if reading the code ma
by TheAndruu 11y ago
It's better to write your code so that it's easy for a later person to understand it while reading.
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`.
- deleted 11y ago[deleted]
- pj_mukh 11y agoThank 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 http://s45.radikal.ru/i108/0902/0f/000d1c614e24.gif
- aeharding 11y agoAlso known as yoda conditions [1]. Some linters have options to enforce non-usage as a best practice [2]. [1] https://en.wikipedia.org/wiki/Yoda_conditions https://en.wikipedia.org/wiki/Yoda_conditions [2] http://eslint.org/docs/rules/yoda http://eslint.org/docs/rules/yoda
- TheAndruu 11y agoNice. 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`