I have the following code:
if (a || b)
{
X();
}
if (a)
{
Y();
}
Can I somehow merge these two if-statements?
This would be better, because if first loop exits because a and b are false, then there is no need to check the second loop-
if(a||b)
{
X();
if(a)
{
Y();
}
}
If your Y() method always returns true you can write:
if(a || Y() || b) X();
But you need take in account that this code will be read by another person, so it should be easy readable and explain its meaning for the other person. So the best solution depends on meaning of a, b, X and Y. And all of the following will be correct with different semantic meanings:
if(a || b)
{
X();
if(a) Y();
}
or
if(a)
{
X();
Y();
}
else if(b)
{
Y();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With