(3 / 4) wrong method return value of enumeration

Posted by xbase on Mon, 06 Jan 2020 21:41:11 +0100

┻┻┳┳┳┳┳ιιιιιιιιιιι

┻┻┳┳┳┳┳ (1 / 4) [code neatness] do you really use enumerations? No!

┻┻┳┳┳┳┳ (2/4) method parameter of wrong use of enumeration

┻┻┳┳┳┳┳ (3 / 4) wrong method return value of enumeration

┻┻┳┳┳┳┳ (4 / 4) wrong use of enumeration inside method body

 

 

Let's continue with the use of enumeration. This article gives an example to illustrate the effect of enumerating the return value of a method on the readability and maintainability of a program.

[first code]

The following code logic is relatively simple: judge the value of busTyp and crdTyp in different situations, assign a value to the string cnlTyp, and finally return the string.

// PpdUtils.java

/**
 * Get channel type
 *
 * @param busTyp
 * @param crdTyp
 * @return
 */
public static String getCnlTyp(String busTyp, String crdTyp) {
    String cnlTyp = "";
    BusTypEnum busTypDict = BusTypEnum.getByKey(busTyp);

    switch (busTypDict) {
        case B2C_CHARGE:
        case B2C_CONSUME_COMPLETION:
        case GWAY_CONSUME_COMPLETION:
        case B2C_ENT_CONSUME_COMPLETION:
        case B2C_ENT_REPAYMENT:
        case PGW_GW_COMPLETION:
        case CREDIT_PAY_COMPLETION:
        case CREDIT_PAY_COMPLETION_REFUND:
            cnlTyp = CnlTypEnum.B2C.getValue();

            break;

        case B2B_CHARGE:
        case B2B_CHARGE_FOR_ENT:
        case B2B_CHARGE_ORDER_ADDITIONAL:
        case B2B_ENT_CONSUME_COMPLETION:
        case B2B_ENT_REPAYMENT:
        case PGW_B2B_COMPLETION:
            cnlTyp = CnlTypEnum.B2B.getValue();

            break;

        case QPAY_CONSUME_COMPLETION:
        case QUICK_CHARGE:
        case PGW_QPAY_COMPLETION:

            if (CardTypeEnum.CreditCard.equals(crdTyp)) {
                cnlTyp = CnlTypEnum.CQP.getValue();
            } else {
                cnlTyp = CnlTypEnum.DQP.getValue();
            }

            break;

        case OCP_CONSUME_COMPLETION:
        case OCP_ENT_CONSUME_COMPLETION:
        case OCP_ENT_REPAYMENT:
        case OCP_REFUND:
            cnlTyp = CnlTypEnum.OCP.getValue();

            break;

        default:
            break;
    }

    return cnlTyp;
}

 

[reconstruction method]

This paper focuses on the return value of methods. So, for the above method, let's leave aside the two input parameters, and don't spray other code.

The way to refactor is to change the return value to the enumeration type CnlTypEnum. Similarly, the type of the variable cnlTyp inside the method is refactored to CnlTypEnum.

In this way, compared with the return String, the caller can intuitively know the specific value range of the return value, thus improving the readability.  

The reconstructed code is:

// PpdUtils.java

/**
 * Get channel type
 *
 * @param busTyp
 * @param crdTyp
 * @return
 */
public static CnlTypEnum getCnlTyp(String busTyp, String crdTyp) {
    CnlTypEnum cnlTyp = null;
    BusTypEnum busTypDict = BusTypEnum.getByKey(busTyp);

    switch (busTypDict) {
        case B2C_CHARGE:
        case B2C_CONSUME_COMPLETION:
        case GWAY_CONSUME_COMPLETION:
        case B2C_ENT_CONSUME_COMPLETION:
        case B2C_ENT_REPAYMENT:
        case PGW_GW_COMPLETION:
        case CREDIT_PAY_COMPLETION:
        case CREDIT_PAY_COMPLETION_REFUND:
            cnlTyp = CnlTypEnum.B2C;

            break;

        case B2B_CHARGE:
        case B2B_CHARGE_FOR_ENT:
        case B2B_CHARGE_ORDER_ADDITIONAL:
        case B2B_ENT_CONSUME_COMPLETION:
        case B2B_ENT_REPAYMENT:
        case PGW_B2B_COMPLETION:
            cnlTyp = CnlTypEnum.B2B;

            break;

        case QPAY_CONSUME_COMPLETION:
        case QUICK_CHARGE:
        case PGW_QPAY_COMPLETION:

            if (CardTypeEnum.CreditCard.equals(crdTyp)) {
                cnlTyp = CnlTypEnum.CQP;
            } else {
                cnlTyp = CnlTypEnum.DQP;
            }

            break;

        case OCP_CONSUME_COMPLETION:
        case OCP_ENT_CONSUME_COMPLETION:
        case OCP_ENT_REPAYMENT:
        case OCP_REFUND:
            cnlTyp = CnlTypEnum.OCP;

            break;

        default:
            break;
    }

    return cnlTyp;
}

Topics: Java