Function with multiple if or with a "big" return?
I would like to know if one of this two solution is better than the other :
Version 1:
bool myFunction()
{
if (A)
return false;
if (B)
return false;
if (C)
return false;
if (D)
return false;
return true;
}
Version 2 :
bool myFunction()
{
return (!A && !B && !C && !D);
}
I guessed that version 2 may be a bit less efficient as we may have to
calculate the whole boolean expression to know if it's true or false. In
the first version, if A is false, it returns false, without calculating B,
C or D. But I find second version much more readable.
So what is the best way to do that ?
Thank you.
No comments:
Post a Comment