An elegant way to code if (10 <x <20)
6 answers
No, but you can rearrange it to make it better, or write a wrapper if it annoys you:
if (InRange(x, 10, 20)) { ... }
Or, as Karl says:
if (new Range(10, 20).contains(x)) { ... }
Although personally I don't see the point. This is a useless abstraction. The bare boolean operator is quite obvious.
Although, now that I think about it, and in light of Karl's comment below, there are times when Range is a perfectly valid and useful abstraction (for example, when working with Feeds). So, depending on the semantics x
, you might want an abstraction.
+5
source to share