Else If chaining with unrelated expressions is bad style
Related expression chaining is GOOD
if (a == A)
X;
else if (a == A2)
Y;
else
Z;
Unrelated expression chaining is BAD
if (a == A)
X;
else if (b == B)
Y;
else
Z;
Nested Equivalent
if (a == A)
X;
else
{
if (b == B)
{
Y;
}
else
{
Z;
}
}
Equivalence
Chained | Nested |
---|---|
if (A) |
X;
else if (B)
Y;
else
Z; | if (A)
X;
else
{
if (B)
Y;
else
Z;
} |
Truth table
| A | B | Chained | Nested | || | 1 | 1 | X | X | | 1 | | X | X | | | 1 | Y | Y | | | | Z | Z |