LimaNightHawk
LimaNightHawk

Reputation: 7083

How can I avoid using method selector arguments (flag/boolean arguments) in Java

I've just finished Clean Code and one of the precepts is to avoid passing boolean arguments as functional selectors, e.g.,

Instead of this:

public double calculateWeeklyPay(boolean overtime) {
    double pay = calculatePay();
    if (overtime) {
         return pay *= 1.5;
    }
    return pay;
}

We should do this, and let the consumer call the appropriate method:

public double calculateWeeklyPay() {
    return calculatePay();
}

public double calculateWeeklyPayWithOvertime() {
    double pay = calculateWeeklyPay();
    return pay *= 1.5;
}

But what should I do in more complicated methods where splitting up the method will mean lots of duplicate code?

What should be done here:

public double calculateWeeklyPay(boolean overtime) {

    double pay = calculatePay();
    pay = modifyPay1(pay);
    pay = modifyPay2(pay)
    pay = modifyPay3(pay)
    if (overtime) {
        pay = pay *= 1.5;
    }
    pay = modifyPay4(pay);
    pay = modifyPay5(pay);
    pay = modifyPay6(pay);

}

Upvotes: 6

Views: 2021

Answers (3)

Seelenvirtuose
Seelenvirtuose

Reputation: 20628

You have to distinguish between the public API and the internal API.

For the public API you really should avoid such selector arguments. But for an internal API (the private methods) it is an appropriate mechanism for implementation. So make it like this:

public double calculateWeeklyPayWithOvertime() {
    return calculateWeeklyPay(true);
}

public double calculateWeeklyPayWithoutOvertime() {
    return calculateWeeklyPay(false);
}

private double calculateWeeklyPay(boolean overtime) {
    double pay = calculatePay();
    if (overtime) {
         pay *= 1.5;
    }
    return pay;
}

Upvotes: 9

rbennett485
rbennett485

Reputation: 2163

If there are any blocks of repeated code after you have split the method in two, good practice is to remove the repeated code to a private method and call this private method in place of the repeated code

e.g. rather than

public void trueMethod() {
     //do a few lines 
     // of general stuff

     println("Did the true method");
}

public void falseMethod() {
     //do a few lines 
     // of general stuff

     println("Did the false method");
}

It would be better to do:

private void generalMethod() {
     //do a few lines 
     // of general stuff
}


public void trueMethod() {
     generalMethod();

     println("Did the true method");
}

public void falseMethod() {
     generalMethod();

     println("Did the false method");
}

Upvotes: 0

Arash Saidi
Arash Saidi

Reputation: 2238

Wouldn't you be able to do the same exact thing in the second example?

public double calculateWeeklyPay() {

double pay = calculatePay();
anotherMethod1(pay);
anotherMethod2(pay)
anotherMethod3(pay)

anotherMethod4(pay);
anotherMethod5(pay);
anotherMethod6(pay);
}

public double calculateWeeklyPayWithOvertime() {
double pay = calculatePay();
anotherMethod1(pay);
anotherMethod2(pay)
anotherMethod3(pay)

    pay = pay *= 1.5;

anotherMethod4(pay);
anotherMethod5(pay);
anotherMethod6(pay);
}

Upvotes: 0

Related Questions