When I want to execute some code under the condition that a variable has one of two (or more) values I can use the OR operator:
if (var == x || var == y) {
DoSomething();
}
But I'm not sure whether I should do this directly, when var is a getXY() function, that might even include some expensive checks:
// Is this good style?
if (getXY() == x || getXY() == y) {
DoSomething();
}
Is there some specified behaviour whether or not the compiler is optimizing these two function calls to only one for both checks? Or is this implementation specific and I have to check for each case?
Would it be "better" to do the function call first, store the result in var and then do the check on var? What is in general considered good coding style for this?
// Or should I do the check first?
var = getXY();
if (var == x || var == y) {
DoSomething();
}
I'm programming in C, but I would assume this to be a general consideration similar in most languages.