
Don't use the greater than sign in programming - jaxondu
http://llewellynfalco.blogspot.com/2016/02/dont-use-greater-than-sign-in.html?m=1
======
TheAndruu
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`.

~~~
aeharding
Also 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
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`

------
Lanari
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.

------
mschoebel
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.

~~~
giomasce
The argument is mostly about double comparisons, i.e., checking for belonging
to an interval.

------
Claudus
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"

------
steaminghacker
Rubbish! greater than is useful if it makes code clearer. Yoda conditionals,
no thanks.

------
barrkel
This leads you to use '0 < x' to test if a number is positive, which to my
eyes looks a bit unnatural.

~~~
daveguy
This. I prefer these rules: variable first sorted by size of comparison.

x > 5 && x < 10

------
giomasce
In Python you even have the shortcut 5 < x < 10.

~~~
jimcsharp
This is the first programming hangup I can remember having. Thinking "Why
can't I just write it like this?"

~~~
bryanlarsen
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...

------
jonesb6
Argument is for improved code clarity, not performance etc. Definitely
something to think about. Thanks for the post.

------
garethrees
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.

------
kmm
Is this satire?

------
ch4s3
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.

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)

------
makecheck
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.

~~~
dsego
Also known as Yoda conditions.

[https://en.wikipedia.org/wiki/Yoda_conditions](https://en.wikipedia.org/wiki/Yoda_conditions)

------
dfan
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.

------
jensnockert
Except then you start using floating-point and then the transformations do not
work anymore.

> 1 < nan

=> false

> nan > 1

=> false

------
gegtik
Cute, but start by lobbying the Mathematics field and once they do away with
">" the programming field will naturally follow.

------
david90
It's all about programming practice. I do like the suggestion `between(x1,
x2)`.

~~~
yitchelle
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.

~~~
Shengbo
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.

~~~
mc808
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.

------
dimman
But '>' > '<'?

------
draw_down
Wow, I completely do not understand.

