Reputation: 11464
Is there a much efficient or better way to rewrite this code?
if (condition 1 = true)
{
// Call to function 1
}
if (condition 2 = true)
{
// Call to function 2
}
Upvotes: 1
Views: 71
Reputation: 95334
In general, no.
If the called functions re-evaluate the condition, then perhaps yes, by lifting the part of the function that re-evaluates the condition out of the function, since the condition is known to be true. (This is called "partial evaluation").
If condition1 and condition2 have a relationship, then perhaps yes. Imagine that condition1 is composed of several terms
c1a and c1b and .. c1n
and that c2 is composed of
c2a and c2b and ... c2m
If some subset of the c1i implies some subset of the c2j, then you can evaluate the conditions more efficiently:
if (c1isubset) {
if (c1irest)
{ // call to function 1
}
endif
if (c2jrest)
{ // call to function2)
}
}
Of course, it may be that a subset of c2j implies a subset of c1i, and then you'd swap the order of tests around.
Upvotes: 1
Reputation: 346
I think that short answer is NO. Long answer depends on conditions and functions.
Upvotes: 3