I was wondering if there is any way to simplify the following complicated branching structure in C#? Note that "do C;" appears twice.
if (condition1)
{
if (condition2)
{
do A;
if (condition3 on result of doing A)
{
do B;
}
else
{
do C;
}
}
else
{
do C;
}
}
else
{
do D;
}
do E;
You can simplify it so that
C()only appears once.Your code is equivalent to
Where the
A()method returns abooland is equivalent to yourcondition3 on result of doing A).Looking at this simplification, note that you can replace:
With:
So the final code would look like this:
It's not a huge improvement, but it now mentions
C()only once.You could also write it as a switch but personally I don't think this is an improvement (I know that this is a subjective matter):
NOTE: It's critical to put
condition2 && A()and notA() && condition2, of course.