by

Is a number within a range?

The other day I saw this on a pull request:

1start(appointment) <= now && end(appointment) >= now

I find this a bit jarring to read and avoid it in my code. Instead I prefer something like the following:

1start(appointment) <= now <= end(appointment)

This predicate, closer to the mathematical predicate, reads better to me. It's easier to see that now needs to be in between two limits. This syntax is allowed in some languages such as Python.

But many other languages don't allow this, so I go for the next best thing:

1start(appointment) <= now && now <= end(appointment)

And now… it was only while writing this that I realised: sometimes there is another great alternative, modelled after the following mathematical predicate:

1now ∈ [start(appointment), end(appointment)]

This can be translated directly to some languages too, such as this Ruby example:

1(start(appointment)..end(appointment)).include?(now)