7 ms·
Don't use the greater than sign in programming
- jonesb6 11y agoArgument is for improved code clarity, not performance etc. Definitely something to think about. Thanks for the post.
- david90 11y agoIt's all about programming practice. I do like the suggestion `between(x1, x2)`.
- yitchelle 11y agoWhen someone says that, I always get confuse as to whether you meant an inclusive or an exclusive between. I think the mathematicians uses the square brackets to indicate that intention.
- Freak_NL 11y agoThat is exactly why we have <, <= (≤), >, and >= (≥) in programming languages. It is a lot less verbose than saying exclusiveBetween(x, 5, 10).
- Shengbo 11y agoThat could actually be an interesting linguistics problem. In spoken languages, "between" is usually inclusive unless context suggests otherwise. It can easily be a source of misunderstanding if the context clues are insufficient or the recipient has trouble picking up on them. I wonder if there are any languages that have different words for inclusive/exclusive "between" so people don't have to rely on implicit clues.
- mc808 11y agoOne possible convention would be to use "in-between" for the exclusive between. It's probably impossible to convince the whole English-speaking world to adopt it, but it would be more natural than "exclusive between" in code.
- TheAndruu 11y agoAgree. Perhaps it be better-named 'betweenInclusive(firstValue, secondValue)' and 'betweenExclusive(firstValue, secondValue)'
- agentultra 11y agoThere are a few notations for intervals in maths; https://en.wikipedia.org/wiki/Interval_%28mathematics%29 https://en.wikipedia.org/wiki/Interval_%28mathematics%29 There are arithmetic operators for intervals and IEEE standards! https://en.wikipedia.org/wiki/Interval_arithmetic#Elementary_functions https://en.wikipedia.org/wiki/Interval_arithmetic#Elementary...
- carlob 11y agohttps://reference.wolfram.com/language/ref/Between.html https://reference.wolfram.com/language/ref/Between.html Which is particularly useful for symbolically representing queries for entities. EntityClass["Element", "AtomicNumber" -> Between[{2, 3}]]
- mschoebel 11y agoHave you ever in real life hear someone say "If 80kg is less than your weight, then you are overweight." Probably not. We say: "If your weight is more than 80kg then you are overweight." When talking we are used to first mention "x", and after that the numerical value we compare it to. No matter if it's less than or larger than what we want to compare it to. If you swap these around, then whoever listens to you - or reads your code - will take a few seconds longer to understand what you mean.
- giomasce 11y agoThe argument is mostly about double comparisons, i.e., checking for belonging to an interval.
- giomasce 11y agoIn Python you even have the shortcut 5 < x < 10.
- jimcsharp 11y agoThis is the first programming hangup I can remember having. Thinking "Why can't I just write it like this?"
- bryanlarsen 11y agoYou just triggered an old man anecdote. We got our first computer on the morning of Dec 25th. We figure out how to plug it into the TV and we turn it on. It turns on, and we get a flashing cursor. Now what? Aren't you supposed to be able to play games on these? The only other thing we have is the BASIC manual. Let's start reading this. So I play with it for a bit, writing programs like '10 print "Bryan is the Master of the Universe"; 20 goto 10'. On the 26th I decide that the first program I should write is a program to randomly pick BINGO numbers. It looks something like this: 10 X=RND(75) 20 IF X=1 OR 2 OR 3 OR 4 OR 5 OR 6 OR 7 OR 8 OR 9 OR 10 OR 11 OR 12 OR 13 OR 14 OR 15 THEN PRINT "B" ... It took me a couple of hours to get IF statement to work. But I did, and here I am over 30 years later...
- giomasce 11y agoI did not know about this instead, and the first time I knew about it I would not believe my friend telling me. I had to try.
- Lanari 11y agoThis post is basically saying "the simpler way is what I think is the simpler way", while in reality everyone see a way simpler then other. Since it doesn't hurt the performance there's no need to advice a way over another, the first way that come up on your head is the simpler for you. Go ahead and finish your project fast mate.
- jensnockert 11y agoExcept then you start using floating-point and then the transformations do not work anymore. > 1 < nan => false > nan > 1 => false
- barrkel 11y agoThis leads you to use '0 < x' to test if a number is positive, which to my eyes looks a bit unnatural.
- daveguy 11y agoThis. I prefer these rules: variable first sorted by size of comparison. x > 5 && x < 10
- garethrees 11y agoI do exactly this in my own code, for one-sided bounds like "if (0 <= x)" as well as for two-sided bounds like "if (0 <= i && i < length)". But when collaborating with other programmers, I follow the majority style. There are lots of programmers who prefer to write comparisons with the varying item on the left and the constant item on the right, and "varying item is on the left" is just as valuable a hint to them as "lower item is on the left (if expression is true)" is to me. So I wouldn't elevate a personal stylistic preference to a rule: this will lead to pointless arguments. Writing code that's clear to all the programmers on a project should be the goal.
- TheAndruu 11y agoIt'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`
- draw_down 11y agoWow, I completely do not understand.
- deleted 11y ago[deleted]
- steaminghacker 11y agoRubbish! greater than is useful if it makes code clearer. Yoda conditionals, no thanks.
- Claudus 11y agoI spotted the incorrect one instantly. Why? Because there are two good ways to write this correctly, and he chose to not use one, and to use the other incorrectly in in his example. 5 < x && x < 10 10 > x && x > 5 Both seem equally readable to me. His argument also seems predicated on the assumption that numbers are always increasing. If x > 10 relax() else countdown(x) // easier to read "if timer is greater than 10, relax, otherwise, countdown" If 10 < x relax() else countdown(x) // harder to read "if 10 is less than timer, relax, otherwise, countdown"
- makecheck 11y agoFor the range case, languages like Python got it right because the statement "1 < x < 5" means exactly what it would seem to mean. For other languages I’m not sure it matters for inequalities. For equality though, I always use the form "if (constant == x)" as opposed to "if (x == constant)" because in C it guarantees that the compiler will trip over an accidental-assignment typo. That is, "if (constant = x)" is a compilation error but "if (x = constant)" might be missed.
- dsego 11y agoAlso known as Yoda conditions. https://en.wikipedia.org/wiki/Yoda_conditions https://en.wikipedia.org/wiki/Yoda_conditions
- kmm 11y agoIs this satire?
- dfan 11y agoI picked up the "always use the less-than sign when you're comparing" rule of thumb from something written by Bjarne Stroustrup, though I can't find an online reference to cite. I don't always do it for single comparisons, but I do whenever there are multiple ones such as checking for whether a value is in a certain range.
- dimman 11y agoBut '>' > '<'?
- gegtik 11y agoCute, but start by lobbying the Mathematics field and once they do away with ">" the programming field will naturally follow.
- ch4s3 11y agoI can see the utility for intervals, but for simple checks I think clarity of intent trumps all. Sometimes if foo.value > bar makes more sense. THis is especially true when foo is the subject of the method. Someone already submitted a PR to the Ruby style guide for this, and I can't express how strongly I disagree. https://github.com/bbatsov/ruby-style-guide/pull/546 https://github.com/bbatsov/ruby-style-guide/pull/546