Live data from Hacker News

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

llewellynfalco.blogspot.com

41–50 of 96 posts

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

#41
post #29
post #8

Ah, https://en.wikipedia.org/wiki/Yoda_conditions

Should I be worried that such a trivial thing has a name and a Wikipedia entry? What is the actual relevance here?

> Should I be worried that such a trivial thing has a name and a Wikipedia entry?

It has non-trivial advantages and drawbacks, which are interesting to document.

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

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

Actually this has zero academic value. The entire point is as a working programmer to compensate for ambiguous pitfalls in a language.

You don't need this specific trick in python (as others have pointed out, `5 < x < 10` works), but in general python holds to a similar philosophy of using standardized idioms to make reading code easier. It's called "being pythonic".

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

#43

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…

I agree that this is trivial. But I also write conditionals the way the author suggests. Mostly because that is the way you'd write it if this was math. You'd say 'For x between 5 and 10' and write '5 < x < 10'.

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

#44

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.

This. How often do I ever check if a number if within a range? Very rarely.

But checking if an index is not too high for an array, is something I do very frequently.

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

#45
post #31

I 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’?

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

#46
The bigger problem here is that if you say...

Let's say that I want to check that something is between 5 and 10.

...you did not specify explicitly whether the ends are inclusive/exclusive, and IMHO that (and the off-by-one errors it causes) is far more important.

Related: I've more than once had to ask someone whether "I will be away from 23 to 27" was inclusive or exclusive for the 27, because e.g. "meeting from 10 to 11" usually is exclusive at the upper end.

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

#47
post #4

Earlier quoted context omitted.

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)`.

Is this efficient? I was under the impression (which is likely inaccurate, because I'm no expert on Python) that range returned a generator or a list outright, making this an O(n) operation.

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

#49

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.

In certain branches of mathematics, it is routine to work with very long inequality chains. For example:

a You would then conclude that a I haven't looked at the article, but based on the comments it sounds like the author has this kind of thing in mind. I got the vibe that he was arguing that you shouldn't mix , not that he was arguing that comparisons should only ever be made low-to-high.

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

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

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.

By convention, math is always left to right regardless of the direction of the script it's in, so presumably the operators do the same.
Post reply on HN