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?
It has non-trivial advantages and drawbacks, which are interesting to document.
41–50 of 96 posts
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?
It has non-trivial advantages and drawbacks, which are interesting to document.
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.
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".
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…
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.
But checking if an index is not too high for an array, is something I do very frequently.
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.
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.
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)`.
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.
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.
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.