I really think this is just bikeshedding over style. If we were optimizing for readability and predictability, we'd standardize on the most commonly used format. ``if (x > 5 && x Putting a rule that variables should always be on the left hand side of a comparison also gives you only 2 ways to write any conditional, so it's just as good as getting rid of ``>``. It also has the advantage of being by far the most widesp…
Don't use the greater-than sign in programming (2016)
51–60 of 96 posts
Re: Don't use the greater-than sign in programming (2016)
#52Earlier quoted context omitted.
is there really a naming distinction between ‘>’ and ‘ the ‘greater’ than sign? Interesting if so! I always assumed it was context dependent (i.e x < 10 could be said 10 is greater than x, so it’s still acting as a greater than sign - but I see why I’m probably wrong here). I was interested in seeing an arguement like another poster made, where notation be like x = [0,1] might be proposed.
> is there really a distinction between ‘>’ and ‘ The convention I've always seen is that `>` is "greater than" and ` 2` is "x is greater than 2" and `2 I don't know if the names are reversed in languages that are read right to left?
Re: Don't use the greater-than sign in programming (2016)
#53I had to write a scripting language for a personal project. Since I could create whatever syntax I wanted, I used this: 'if x==4' was written as 'x[4]' 'if x>2 && x 'if x>=2 && x Etc. It was very easy to read and write... at least for me.
What if you wanted ‘if x>2 || x < -2’?
if-not x[-2,2]
Just like you'd write x ∉ [-2,2] with pen and paper.Re: Don't use the greater-than sign in programming (2016)
#54I had to write a scripting language for a personal project. Since I could create whatever syntax I wanted, I used this: 'if x==4' was written as 'x[4]' 'if x>2 && x 'if x>=2 && x Etc. It was very easy to read and write... at least for me.
What if you wanted ‘if x>2 || x < -2’?
x)-2,2(
or x![-2,2]Re: Don't use the greater-than sign in programming (2016)
#55Qualification: Don't use the greater sign when doing range checks. Counter-point: When checking if a variable is greater than a constant, using the less than is very confusing since we read left to right. if(x > LIMIT) { Read "If x exceeds LIMIT, then" The opposite reads like "If the LIMIT is less than x", which sounds strange and emphasizes the wrong thing, implying LIMIT changed, not x.
But if the way you think about the domain is "if x is larger than LIMIT..." you should write it `if (x > LIMIT)`!
Re: Don't use the greater-than sign in programming (2016)
#56I really think this is just bikeshedding over style. If we were optimizing for readability and predictability, we'd standardize on the most commonly used format. ``if (x > 5 && x Putting a rule that variables should always be on the left hand side of a comparison also gives you only 2 ways to write any conditional, so it's just as good as getting rid of ``>``. It also has the advantage of being by far the most widesp…
Indeed. This article reads like someone who doesn't understand inequalities, and proposes a quite possibly hazardous shortcut to it instead. To not understand such basic maths is baffling (this is primary-school stuff), and I wonder at the state of maths education these days to see adults struggling with these concepts.
I have never in over 3 decades of programming ever had to ask myself the question of how to write conditional expressions --- it's something that comes naturally, and in this specific case I would also write it with the variables on the left.
Re: Don't use the greater-than sign in programming (2016)
#57Ha, I've been using this trick for a while, though I wouldn't go so far as to say "don't use the greater-than-sign." Honestly I just wish languages had a cleaner way to write "x is within this range," e.g. "if x in [2,4)"
Swift has it. I'm sure a few languages must have it. if (2...4).contains(x)
$ swift
Welcome to Apple Swift version 4.2.1 (swiftlang-1000.11.42 clang-1000.11.45.1). Type :help for assistance.
1> let x = 3
x: Int = 3
2> if x in 2...4 {}
error: repl.swift:2:6: error: expected '{' after 'if' condition
if x in 2...4 {}
^
Are you sure you didn't mean to use if case? if case 2...4 = xRe: Don't use the greater-than sign in programming (2016)
#58Lately, I am enamored of Ruby x.between?(6,9) or very explicitly x.between?(5.0.next_float, 10.0.prev_float)
(and 7 other ways)
Re: Don't use the greater-than sign in programming (2016)
#59I had to write a scripting language for a personal project. Since I could create whatever syntax I wanted, I used this: 'if x==4' was written as 'x[4]' 'if x>2 && x 'if x>=2 && x Etc. It was very easy to read and write... at least for me.
Re: Don't use the greater-than sign in programming (2016)
#60The number line representation is incorrect. (5 < x && x < 10) represents x has possible values of 6,7,8,9 (assuming x is int), whereas the number line shows that 5 and 10 are inclusive, but that's not the case as per the provided example.