Live data from Hacker News

Don't use the greater-than sign in programming (2016)

llewellynfalco.blogspot.com

11–20 of 96 posts

Re: Don't use the greater-than sign in programming (2016)

#11
post #6

as a CS major im sure this has academic value. as someone who is just learning python outside of a trade job as an engine mechanic, this idea makes me want to glass someone and im not sure why. (5 if we're talking about X, whats wrong with defining the parameters of its constraints in terms of X instead of dancing around the numbers? 5 < X makes it sound like im setting constraints on the number 5.

In python you can just do 5 < x < 10

Re: Don't use the greater-than sign in programming (2016)

#12
Qualification: 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.

Re: Don't use the greater-than sign in programming (2016)

#13
post #4
post #2

Ha, 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)"

In Python we have if 2 <= x < 4.

That... has nothing to do with nemo1618's wish. The python statement 2 4.

Re: Don't use the greater-than sign in programming (2016)

#14
post #2

Ha, 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)

Re: Don't use the greater-than sign in programming (2016)

#15
post #2

Ha, 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)"

SQL has "x between 2 and 4" (for example) which is a lot clearer.

Re: Don't use the greater-than sign in programming (2016)

#16
post #6

as a CS major im sure this has academic value. as someone who is just learning python outside of a trade job as an engine mechanic, this idea makes me want to glass someone and im not sure why. (5 if we're talking about X, whats wrong with defining the parameters of its constraints in terms of X instead of dancing around the numbers? 5 < X makes it sound like im setting constraints on the number 5.

In python you can just do 5 < x < 10

which is also the proper way you would read in any scientific paper, and very easy to read imho.

5 < x && x < 10 is the attempt to replicate this in other languages but falls a bit short

Re: Don't use the greater-than sign in programming (2016)

#18
post #9

This all looks good to me until I'm unwrapping an optional and checking it's length in Swift, which normally looks like: guard let array = obj.someArray, array.count > 0 else { throw SomeError }

`array.count < 1` still sounds reasonable though.

Re: Don't use the greater-than sign in programming (2016)

#20
post #4

Earlier quoted context omitted.

In Python we have if 2 <= x < 4.

That... has nothing to do with nemo1618's wish. The python statement 2 4.

Nobody would ever purposefully write 2 4 … it's a bug. Maybe if the bounds on the example were variable.

But the point of being able to collapse a I don't think I've ever seen it actually used for anything else, and I would in advocate against it, in code review.

Post reply on HN