RFC: Add ‘between’ and ‘in’ to modern day languages

October 1, 2012

One of the recent additions to TeaTrove I am making is adding the between and in operator. This may seem trivial, but they are quite powerful as well as generally better performing, not to mention easier to read. The SQL standard has had these operators for decades because they just make sense. I’m rather surprised by how many modern day languages do not have it. Many of the languages do have support for the in operator, which is a good addition, but it’s not a suitable replacement as the between operator.

For example, consider the following code:

if (getX() <= 5 and getX() >= 9) { }

Although that makes sense, the following is much more readable:

if (getX() between 5 and 9) { }

It is also faster because getX() is only invoked once rather than twice as in the previous example. You could avoid that in the previous example, but using a local variable, but often times that is overkill for a simple check.

The in operator is even more convenient:

if (getX() == 5 || getX() == 7 || getX() == 9 || getX() == 11) { }
// vs
if (getX() in (5, 7, 9, 11)) { }

With the range operator in Groovy, this is possible to simulate the between operator but it does not perform as fast as it has to do a list creation and then a list lookup rather than two conditional checks which is heavily optimized in VMs.

if (getX() in 5..10) { }

Hopefully future languages (Groovy, Javascript, Python, Ruby, etc) can all add the between and in operators in future releases to further empower developers.

One Response to “RFC: Add ‘between’ and ‘in’ to modern day languages”

  1. […] source(Nicolas Hagen) Share this:TwitterFacebookLike this:LikeBe the first to like […]