'How can I refactor the mass if-else statement?

How can I refactor the mass if-else statement?? I have lots of big if-else statements in my app, do I need to refactor all this code or just leave it alone?

if (up > 80 && up < 140)
{
    if (distance)
    {
        go.transform.position = go2.transform.position;
    }
    go.transform.Translate(y, 0, x);
}
else
{
    if (down > 140)
    {
        if (distance)
        {
            go.transform.position = go2.transform.position;
        }
        go.transform.Translate(0, -y, x);
    }
    else if (down < 35)
    {
        if (distance)
        {
            go.transform.position = go2.transform.position;
        }
        go.transform.Translate(0, -y, -x);
    }
    else if (down > 35 && down < 140 && right > 0)
    {
        if (distance)
        {
            go.transform.position = go2.transform.position;
        }
        go.transform.Translate(-x, -y, 0);
    }
    else if (down > 35 && down < 140 && right < 0)
    {
        if (distance)
        {
            go.transform.position = go2.transform.position;
        }
        go.transform.Translate(x, -y, 0);
    }
}

How can I refactor the mass if-else statement?? I have lots of big if-else statements in my app, do I need to refactor all this code or just leave it alone?



Solution 1:[1]

Try this

private static void Foo(bool distance, YourGoClass go, YourGoClass go2, double x, double y, double z)
{
    if (distance) go.transform.position = go2.transform.position;
    go.transform.Translate(x, y, z);
}

And then you can change your code like this

if (up > 80 && up < 140)
{
    Foo(distance, go, go2, y, 0, x);
}
else
{
    switch(down)
    {
        case var _ when down > 140:
            Foo(distance, go, go2, 0, -y, x);
            break;
        case var _ when down < 35:
            Foo(distance, go, go2, 0, -y, -x);
            break;
        default:
            if (right > 0) Foo(distance, go, go2, -x, -y, 0);
            else Foo(distance, go, go2, x, -y, 0);
            break;
    }
}

Solution 2:[2]

You can create a method like this:

private static void Translate(bool distance, YourGoClas go, YourGoClas go2, double x, double y, double z)
{
    if (distance)
    {
        go.transform.position = go2.transform.position;
    }

    go.transform.Translate(x, y, z);
}

And refactor:

if (up > 80 && up < 140)
{
    Translate(distance, go, go2, y, 0, x);
}
else
{
    if (down < 35)
    {
        Translate(distance, go, go2, 0, -y, -x);
    }
    else if (down < 140)
    {
        if (right > 0)
        {
            Translate(distance, go, go2, -x, -y, 0);
        }
        else if (right < 0)
        {
            Translate(distance, go, go2, x, -y, 0);
        }
    }
    else // if (down > 140)
    {
        Translate(distance, go, go2, 0, -y, x);
    }
}

The key is look for code almost equal and run in a separate method. And try to do if conditions in order, only a single comparison when it's allow you separate all cases.

Solution 3:[3]

I think you could apply extract method which reduces duplicated code.

private void setPositionByDistance(boolean distance, Go go, Go go2) {
    if (distance)
    {
        go.transform.position = go2.transform.position;
    }
}

And use early return pattern, which reduces unnecessary nested if-else hierarchy.

Then your code will become better:

if (up > 80 && up < 140)
{
    setPositionByDistance(distance, go, go2);
    go.transform.Translate(y, 0, x);
    return;  // early return
}

if (down > 140)
{
    setPositionByDistance(distance, go, go2);
    go.transform.Translate(0, -y, x);
}
else if (down < 35)
{
    setPositionByDistance(distance, go, go2);
    go.transform.Translate(0, -y, -x);
}
else if (down > 35 && down < 140 && right > 0)
{
    setPositionByDistance(distance, go, go2);
    go.transform.Translate(-x, -y, 0);
}
else if (down > 35 && down < 140 && right < 0)
{
    setPositionByDistance(distance, go, go2);
    go.transform.Translate(x, -y, 0);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Victor
Solution 3