Daily Archives: August 25, 2011

CS101: Always Use LT

Here’s a simple tip! I can’t begin to count how many potential code bugs this has eliminated. It takes some getting used to but once you make it automatic it’s a real help in keeping code and your thinking correct. The tip is this: when you write relational expressions, always use less-than, never use greater-than.

Tip: Always write (B < A), not (A > B).

Make Less Not More

Always write your relational expressions to use less-than and so that a true evaluation puts the lesser quantities on the left of the greater quantities. In rare cases a language doesn’t allow the appropriate syntax, but in most cases you can write the less-than version of any expression. This is especially important when testing a value range (for example, between ‘A’ and ‘Z’).

By the way, I admit to assuming the Euro-American left-to-right bias here. In cultures with strong right-to-left orientation, I hope my logic works in mirror reverse.  The  point is to use instinctive mathematical less-to-greater orientation in relational expressions to help avoid logic errors.

The intent is to apply the left-to-right orientation of the number line to relational expressions. On the (Euro-American) number line, values run from lesser on the left to greater on the right. If you pick a number, L, from the line and pick another number, R, to the right of it, the relation (L < R) is always true and (L > R) is always false.

If the expression logic follows number-line logic, the expression logic makes more sense. For example, if we want to test that x is in the [A–Z] range, one way to write the expression is:

(‘A’ <= x) AND (x <= ‘Z’)

The test value, x, visually falls between the ‘A’ and the ‘Z’. In this form, the expression resembles a common mathematical form:

(‘A’ <= x <= ‘Z’).

Most computer languages do not allow the above syntax, but nearly all allow the first one, which comes very close. Compare this to a common alternative, that places the variable first:

(x >= ‘A’) AND (x <= ‘Z’)

In a complex expression, following mathematical number-line logic can make an expression more clear and help highlight logical errors. For example, consider this (which is not at all untypical code):

((‘A’ <= x) AND (x <= ‘Z’)) OR ((‘a’ <= x) AND (x <= ‘z’)) OR
((‘0’ <= x) AND (x <= ‘9’))

And compare it to this:

((x >= ‘A’) AND (x <= ‘Z’)) OR ((x <= ‘a’) AND (x <= ‘z’)) OR
((x >= ‘0’) AND (x <= ‘9’))

I think it’s much easier to use the first one! In fact, did you spot the error in the second version? The first version is much easier to debug, because you know all the arrows point to the left.

Note that this follows the standard looping conventions:

{J=0;  J < N;  next J}
{J=1; J <= N; next J}

It takes a while to train yourself to use less-than strictly, but once you do, your coding skills will be stronger. You may even find yourself going back and “correcting” old code. It will start to look “wrong” to you!