I write a lot of methods looking a bit like this:
/* myVal must be between 10 and 90 */
int myVal = foo;
if(myVal < 10) { myVal = 10; }
else if (myVal > 90) { myVal = 90; }
Is there a more elegant way of doing it? Obviously you could easily write a method, but I wondered if any languages had a more natural way of setting a constraint, or whether there was something else I’m missing.
Language agnostic, because I’m interested in how different languages might deal with it.
In most programming languages, you could use something like
or simply write a
clip()macro which does the same thing.