On Fri, 2 Oct 2015 07:45 am, John Gordon wrote: > In <87r3le1ht3....@elektro.pacujo.net> Marko Rauhamaa <ma...@pacujo.net> > writes: [...] >> Wouldn't > >> x < 0 or 10 < x > >> be even more visual? > > I don't know what you mean by "more visual". > > In my opinion, when comparing a variable to a constant, it's more natural > to have the variable on the left and the constant on the right, so that's > one strike against this code. > > Another strike is that the code isn't consistent with itself; it puts the > variable on the left in the first comparison, then swaps to the right for > the second comparison.
The alternative is equally inconsistent: for one comparison it uses less than, for the other it uses greater than. I find this discussion about the relative readability of not 0 <= x <= 10 #1 versus 0 < x or x > 10 #2 0 < x or 10 < x to be a good example of people's propensity to invent so-called "rational" justifications for irrational preferences. Honestly, all these people claiming that reading #1 takes them "slightly more effort" than reading #2, or vice versa. Really? You've done an objective test of this? I don't think so. We know from UI testing in other fields that people's *self-reported* efficiency and their *actual* efficiency are almost completely uncorrelated. Back in the DOS versus Macintosh days, when mice were new, DOS users consistently reported that using a mouse was too slow and inefficient compared to the keyboard, but actual objective measurements of the time that they took to do tasks showed the opposite. E.g. subjects were asked to do some task, using the keyboard and then again using the mouse, and (let's say) it actually took them 3 minutes to do it via keyboard and 2 minutes via mouse, they reported that using the keyboard commands was much more efficient, fast and easy. Whether you have #1 or either variation of #2, the time it takes to read and comprehend the expression is likely to be of the order of a few dozen milliseconds, plus or minus a few dozen milliseconds. I expect the variation will be nearly as great as the average time. (But of course, I haven't done objective studies either.) Anyone who has done high-school level maths should be familiar with the notation `0 <= x <= 10`, read as "x between 0 and 10". (Anyone who is not can hardly claim to be fluent in Python -- that's like claiming to be fluent in English while not knowing where and how to use an exclamation mark.) If you want to check for the opposite, x not between 0 and 10, the natural way to do so is with `not`: `not 0 <= x <= 10`. If you prefer `x < 0 or x > 10` for whatever reason, that's fine too. Some people prefer pizza, some prefer kebabs... inventing (spurious) arguments of (imaginary) efficiency ("It takes me half a second less time to chew each mouthful of burger compared to kebab") is unnecessary. But we do love the argument from efficiency. Frankly, as a profession, we programmers are lousy at prioritising. We write pages and pages of convoluted, inconsistent, hard to understand code that can sometimes take hours or even days to comprehend well enough to make even a minor change, while spending dozens of person-hours arguing about which expression saves them a microsecond or two of reading time. Yay us! -- Steven -- https://mail.python.org/mailman/listinfo/python-list