I was teach on mathematics that on real line small numbers are on left and big numbers are on right. Therefore, I think the comparison 0<1 is more natural than 1>0, and in general, x less than y is easier to read than y greater than x. Is there any statistics if there are less bugs on programs that uses only less than comparisons than programs uses greater than comparisons or both?

  • 1
    $\begingroup$ This isn't exactly a question about how to teach or about best educational practice. With a bit of modification, though, it could easily fit in this forum. (Plus I have an answer I would love to give if it could be made to fit) $\endgroup$
    – Ben I.
    Dec 5 '19 at 19:24
  • $\begingroup$ @BenI A little modification. If we can show that it has high probability of being true, then it should inform teaching. $\endgroup$ Dec 7 '19 at 8:16

I think the typical order has more to do with grammatical conventions actually.

Joe is heavier than Bob

Joe is the subject, the one we're talking about; Bob is just an object we compare against.

Joe weighs more than 90kg


90kg is less than Joe weighs

The second one is a very odd sentence.

[subject] [verb] [object]

[variable] [operator] [constant]

This would be a pretty normal construction, such as:

x < 0


x > 0

but odd would be:

0 < x

Because the value of 0 isn't changing. Zero is not the "subject" of what we're doing. x is the subject, the variable, the interesting thing.

So what if we're comparing two variables?

x < y

It's possible both of these are new values to us, but intuitively, this code feels like it's more about x than about y. Consider:

if x < y: do something
if x > z: do something

Now, clearly, x is our main subject, running against various other variables to see if something needs to be done.

So, my impression is that any preference for < over > or otherwise pales in comparison for the preference to put the "subject" of the comparison on the left.

  • $\begingroup$ While your statement is true for many written and spoken languages, I think that programming has different needs. Also, some languages reverse word order, so 90kg < Joe would be normal. When I am scanning code, I like to locate the constants. I can guess that the other part is a variable. Constants dictate what will happen, variables are just along for the ride. $\endgroup$
    – Scott Rowe
    Dec 15 '19 at 1:14

First, I doubt that there are any such statistics because the effect would seem to be so small as to be overwhelmed by other, more common, things. You could, perhaps, create an instrument (as in a compiler) to look for such things and look for effects in, say, unit tests of the code. But that implies that a lot of things come together.

But, I would also suggest that your basic understanding of human psychology may be naive. It could well be that there are people who actually think more effectively in "greater than" mode. As a mathematician, I've often been confused by left-right and by east-west, especially when they all show up in the same context. But it is, I'm pretty sure, because, I look at things with a "frameless" view. What is left and what is right depends on where I think of myself as viewing from.

Also, some times it is just more "natural" to express things with greater than, as in "error > 0.05".

So, I think a teaching "rubric" that punished students for using one form over the other would be misplaced. It would, in some situations, lead the user to perform an extra step in their thinking, and that, alone, could lead to an error.

  • $\begingroup$ There has been research on whether using > vs >= leads to more or fewer errors. (I think the upshot is that people make fewer errors when they use one method consistently instead of trying to be clever.) We often must use "open intervals" where the starting point is an absolute number, like >= 0, and the upper bound is a < comparison, like: 4.9999 is OK but 5.0000 is right out. I used to teach that routinely, from the section of the textbook on Range Comparisons. Not sure anyone really got it, but to me it made sense. Of course, you could say: IF 5.0 NOT <= x THEN... DeMorgan and all that. $\endgroup$
    – Scott Rowe
    Dec 15 '19 at 1:08

Similar to Buffy, I suspect that which use is more natural depends on the framing of the problem.

while (countdown > 0)

makes perfect sense. By contrast,

while (0 < countdown)

feels less intuitive, if only because there is a bias towards leaving the variable on the left side of an expression.

I think that the key (and this is what I would communicate to my students) is to make whatever choices you make carefully reflect the mental model of the problem. This should create the clearest code, and produce the fewest errors.

  • $\begingroup$ I guess I tend to use "Yoda Comparisons" because I was trying to avoid the C error of using = when I meant ==. If I try to assign x to zero, the compiler will not let me. Also, constants tend to be shorter than variables, especially in an OO context. So several comparison lines in succession would all be something like "0 < abcdef.ghe.jkmnop.qrstu(wxyz)" and it would be easy to read the thing compared against instead of having to hunt for it at the far right of the lines. So, to me "while (0 < whatever)" is good. I know the big long string is the variable part, I want to know the constant. $\endgroup$
    – Scott Rowe
    Dec 15 '19 at 0:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.