DEV Community

Thibaut Andrieu
Thibaut Andrieu

Posted on • Updated on

Tiny Tips : Ordering your inequalities in conditions

In high school, when you want to indicate x is between 0 and 5, you wrote:

0 < x < 5.

In programming, you end up with:

if ( 0 < x && 5 > x )
Enter fullscreen mode Exit fullscreen mode
if ( x > 0 && x < 5 )
Enter fullscreen mode Exit fullscreen mode
if ( 0 < x &&
     5 > x )
Enter fullscreen mode Exit fullscreen mode

Where it is hard to understand that x should be in the interval [0, 5].

Personally, I always write it like this:

if ( 0 < x && x < 5 )
Enter fullscreen mode Exit fullscreen mode

Top comments (0)