ABC123
ABC123

Reputation: 1065

Is there a good way to refactor methods that use duplicate conditional statements?

I have two similar methods with duplicate conditions and duplicate else blocks. I'd like to refactor this to share the same logic and else blocks, but call the different methods. How can I do this?

public void entryPoint1(...)
{
    if(nullCheckStuff) {}
        method1(stuff);
        method2(stuff);
    } else {
        //log error
    }
}

public void entryPoint2(...)
{
    if(nullCheckStuff) {
        method2(stuff);
    } else {
        //log error
    }
}

Upvotes: 0

Views: 265

Answers (3)

Progs
Progs

Reputation: 737

So you have your code like this:

public void entryPoint1(...)
    {
        if(nullCheckStuff) {}
            method1(stuff);
            method2(stuff);
        } else {
            //log error
        }
    }

public void entryPoint2(...)
    {
        if(nullCheckStuff) {
            method2(stuff);
        } else {
            //log error
        }
    }

You have to take out the if block and put it in another function, let say checkNullStuff():

public bool checkNullStuff(<object, string, whatever> condition, int entrypoint) {
    bool everythingOk = true;
    //checks if null
    if(condition) {
      return !everythingOk;
     }
    //executes common methods
     method1(stuff);
    //if not empty check what to do

    switch(entrypoint) {
      case 1:
         method2(stuff);
        break;
      case 2:
        method3(stuff);
        break;
      default:

        everythingOk = false;
    }
    return everythingOk;
}

Why i used a switch instead of an if, well if your entrypoints grows and more common methods are needed to be executed and the entrypoint function:

public void entryPoint1(...)
{
    // here we check if went wrong, otherwise the functions were executed
    if(!checkNullStuff(nullCheckStuff, 1)) {}
       // code for when nullCheckStuff was not what we exepected
    } 
}

Upvotes: 0

yuppie-flu
yuppie-flu

Reputation: 1490

If you use Java 8 or above, a possible solution would be to use lambdas. You define internal function with the common logic, which takes a Runnable as an argument:

private void commonLogic(Runnable action)
{
    if(nullCheckStuff) {
        action.run();
    } else {
        //log error
    }
}

Then your original functions look simply like:

public void entryPoint1()
{
    commonLogic(() -> { method1(); method2()});
}

public void entryPoint2()
{
   commonLogic(() -> method2());
}

You will also probably need to add more parameters to commonLogic() function to pass data required for nullCheckStuff expression and error handling block.

Upvotes: 3

Abs
Abs

Reputation: 300

If your methods doesn't already have too much parameters, you could refactor that way:

public void commonEntryPoint(..., boolean m1Condition) {
    if(nullCheckStuff) {
        if (m1Condition) {
            method1(stuff);
        }
        method2(stuff);
    } else {
        //log error
    }
}

Cheers!

Upvotes: 0

Related Questions