Re:Don't use the greater than sign in programming
by TheAndruu on Monday February 22, 2016 @08:39AM
(#11150678)
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`.
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 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`
Re:Don't use the greater than sign in programming
by Lanari on Monday February 22, 2016 @06:39AM
(#11150123)
This 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.
Re:Don't use the greater than sign in programming
by mschoebel on Monday February 22, 2016 @04:29AM
(#11149610)
Have 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.
Re:Don't use the greater than sign in programming
by Claudus on Monday February 22, 2016 @09:12AM
(#11150859)
I 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"
Re:Don't use the greater than sign in programming
by bryanlarsen on Monday February 22, 2016 @09:14AM
(#11150867)
You 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...
Re:Don't use the greater than sign in programming
by garethrees on Monday February 22, 2016 @08:15AM
(#11150550)
I 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.
Re:Don't use the greater than sign in programming
by ch4s3 on Monday February 22, 2016 @11:20AM
(#11151879)
I 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.
Re:Don't use the greater than sign in programming
by makecheck on Monday February 22, 2016 @09:13AM
(#11150864)
For 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.
Re:Don't use the greater than sign in programming
by dfan on Monday February 22, 2016 @09:55AM
(#11151131)
I 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.
Re:Don't use the greater than sign in programming
by yitchelle on Monday February 22, 2016 @06:39AM
(#11150120)
When 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.
Re:Don't use the greater than sign in programming
by Shengbo on Monday February 22, 2016 @08:06AM
(#11150508)
That 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.
Re:Don't use the greater than sign in programming
by mc808 on Monday February 22, 2016 @08:41AM
(#11150688)
One 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.
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`.
[1] https://en.wikipedia.org/wiki/Yoda_conditions
[2] http://eslint.org/docs/rules/yoda
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`
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.
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.
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"
x > 5 && x < 10
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...
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.
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
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.
https://en.wikipedia.org/wiki/Yoda_conditions
> 1 < nan
=> false
> nan > 1
=> false
There are arithmetic operators for intervals and IEEE standards! https://en.wikipedia.org/wiki/Interval_arithmetic#Elementary...
Which is particularly useful for symbolically representing queries for entities.
EntityClass["Element", "AtomicNumber" -> Between[{2, 3}]]