Live data from Hacker News

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

llewellynfalco.blogspot.com

31–40 of 96 posts

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

#32
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)

Does that work for floats?

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

#34

I use this. I can read lines reading left to right with only very quickly now, because of the visualization of a numberline in my head. Any other arrangement, and I have to slow down to my old, normal processing, and reason very carefully about the code, lest a subtle bug slip by. For similar reasons, I find this notation helps me prevent errors, because they become visually detectable.

Sure if you have

    5 
you can use the number line visualization very quickly, but it breaks down if the code is in fact

    x 
It means the same thing. But it's not obvious.

So the number line analogy is incomplete and even when there is only </<= you still need careful reasoning.

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

#35
post #21

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

If you read Pull the lever when x aloud, you must read it as "Pull the lever when x is less than zero." Thus, " " is "greater than", for that reason specifically. Also, Unicode calls it U+003C LESS-THAN SIGN and U+003E GREATER-THAN SIGN. At least, that's the terms that the article is using, and I think there's sufficient context to identify that, and go along w/ it.

Well, and I don’t mean to be strictly pebdabtic, but you could read it as “when 0 is greater than x”, but of course it takes more overhead and I see why I was wrong. Thanks, it’s getting late today so I’m glad I managed to learn something before the days up.

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

#36
post #21

Earlier quoted context omitted.

Where is the article using a >, aside from examples of what it is advocating against?

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.

Yes, there absolutely is. Because we write so many languages left-to-right, '>' denotes 'greater than'. Perhaps right-to-left written languages have mathematical comparison operators with opposite names.

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

#37

Earlier quoted context omitted.

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.

> I don't think I've ever seen it actually used for anything else, and I would in advocate against it, in code review.

Yes, this would be a terrible practice. But that's what Python offers. The desire to express that a variable's value lies within a range is very common, and is presumably the reason for Python's bad choice. But that doesn't make Python's syntax a good response to the desire. The syntax expresses a conceptual mess; you're relying on people only using a tiny subset of what's there.

Contrast lisp, where you actually can express the concept of a range, as (Contrast Ruby, which offers almost every convenience in ranges, including literal notations, that you could ask for.

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

#39

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…

> more productive for you to hold another meeting

Are you serious? These are just 2 neat ways for expressing common boundaries conditions. The point is it's more readable instead of using '>'. That is all.

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

#40
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.

There's also `if x in range(2,4)`, which translates to `if x in the interval [2, 4)`.
Post reply on HN