Shiladittya Chakraborty
Shiladittya Chakraborty

Reputation: 4418

Refactoring common code in switch code

switch (customerPaymentInfo.getPtType()) {
        case CASH:
            test();
            otherMethod1();
            break;
        case CARD:
            test();
            otherMethod2();
            break;
        default:
            throw new IllegalArgumentException("Payment Type is Not Correct.");
    }

In the above code I am having one common method which one executing for both case CASH or CARD.

Is there any possibilities in switch case to use it single times?

For eaxmple in case of If block we can write code something below :

if (customerPaymentInfo.getPtType().equals("CASH") || customerPaymentInfo.getPtType().equals("CARD")) {
    test();
}

Upvotes: 2

Views: 1484

Answers (2)

Axel
Axel

Reputation: 14159

Perhaps it would be good to look at this from another angle. Why is there a switch at all? It looks like otherMethod1() and otherMethod2() are doing the same thing in different ways, dependeing on the payment type.

I image othMethod1() could be something like processPaymentByCash() and processPaymentByCard(). Then the differences in implementation should rather be handled by the different classes for those payment types:

class PaymentCash extends Payment {
    processPayment() {
        test();
        // Code from othermethod1
    }
}

class PaymentCard extends Payment {
    processPayment() {
        test();
        // Code from othermethod2
    }
}

class PaymentWhatever extends Payment {
    processPayment() {
        throw new IllegalArgumentException("Payment Type is Not Correct.");
    }
}

Above switch would then simply be replaced by this one liner:

customerPaymentInfo.getPtType().processPayment();

Now, you still have two calls to test() in your code, but IMHO this really all depends on the bigger context of your code.

It also looks like the different payment types should rather be implemented as enum values.

Upvotes: 3

PentaKon
PentaKon

Reputation: 4636

One way to approach this would be to use the cascading property of switch

switch (customerPaymentInfo.getPtType()) {
    case CASH:
    case CARD:
        test();
        break;
    default:
        throw new IllegalArgumentException("Payment Type is Not Correct.");
}

switch (customerPaymentInfo.getPtType()) {
    case CASH:
        otherMethod1();
        break;
    case CARD:
        otherMethod2();
        break;
    default:
        throw new IllegalArgumentException("Payment Type is Not Correct.");
}

Now however you have redundancy on switch statements.

Another way would be to first check of the IllegalArgumentException, then run test() then do a switch statement:

if (!customerPaymentInfo.getPtType().equals(CASH) || !customerPaymentInfo.getPtType().equals(CARD))
    throw new IllegalArgumentException("Payment Type is Not Correct.");
test();
switch(customerPaymentInfo.getPtType()) {
  //switch code
}

Finally, a Java8 only soltion, if you have many such cases (where they have a common call and a specific call) you might group them for the common call and for the specific part you could use a map of Enums and Functions.

switch (customerPaymentInfo.getPtType()) {
    case CASH:
    case CARD:
        test();
        functionMap.get(customerPaymentInfo.getPtType()).apply(holderOfOtherMethod);
        break;
    default:
        throw new IllegalArgumentException("Payment Type is Not Correct.");
}

Upvotes: 0

Related Questions