boolean f(boolean A, boolean B, boolean C, boolean D, boolean E)
{
if (A)
{
k();
if (B)
{
m();
if (C)
{
n();
if (D)
{
p();
if (E)
{
q();
return true;
}
else
{
r();
return false;
}
}
else
{
s();
return false;
}
}
else
{
t();
return false;
}
}
else
{
v();
return false;
}
}
else
{
w();
return false;
}
}
Probably only by flattening the ifs by evaluating the conditions more than once:
if (A) k(); else w();
if (A && B) m(); else if(A && !B) v();
if (A && B && C) n(); else if (A && B && !C) t();
if (A && B && C && D) p(); else if (A && B && C && !D) s();
if (A && B && C && D && E) q(); else if (A && B && C && D && !E) r();
return (A && B && C && D && E);
Without knowing more about the problem you are solving, I would rewrite it as
boolean f(boolean A, boolean B, boolean C, boolean D, boolean E)
{
if (A) k();
if (A && B) m();
if (A && B && C) n();
if (A && B && C && D) p();
if (A && B && C && D && E) { q(); return true; }
if (A && B && C && D && !E) { r(); return false; }
if (A && B && C && !D) { s(); return false; }
if (A && B && !C) { t(); return false; }
if (A && !B) { v(); return false; }
if (!A) { w(); return false; }
}
This, in my opinion, makes it somewhat easier to follow the scenarios.
However this is still absolutely horrible.
What you most likely want is some kind of algorithm pattern where the different behaviors are impemented as different classes which implement the same interface and you would select the behavior based on polymorphism or the algorithm would be injected during object creation.
Basically every method taking more than one boolean argument is a code smell.
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