Make the code elegant: remember a code micro-refactoring practice | JD Cloud technical team

1. Requirement development and modification of code

During a requirement development, I encountered the following method code:

private OrderShoudSettlementAmount getOrderShoudSettlementAmount(OrderDTO orderMain, List<SettlementDetail> details) {
    OrderShoudSettlementAmount settlementAmount = new OrderShoudSettlementAmount();
    // 应结金额=33021-33002-32003+32001-31001
    // 货款佣金=33005+33002+32003+31001
    long feeMoney33021 = 0;
    long feeMoney33002 = 0;
    long feeMoney32003 = 0;
    long feeMoney32001 = 0;
    long feeMoney31001 = 0;
    long feeMoney33005 = 0;
    for (SettlementDetail settlementDetail : details) {
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_FREIGHT_ZS_NOSETTLE.getVal())) {
            feeMoney33021 += settlementDetail.getOassMoney();
        }
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_FREIGHT_YJ_ZS_NOSETTLE.getVal())) {
            feeMoney33002 += settlementDetail.getOassMoney();
        }
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_GOODS_YJ_WJTZ.getVal())) {
            feeMoney32003 += settlementDetail.getOassMoney();
        }
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_GOODS_NOSETTLE.getVal())) {
            feeMoney32001 += settlementDetail.getOassMoney();
        }
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_BDYJ_YJ_NOSETTLE.getVal())) {
            feeMoney31001 += settlementDetail.getOassMoney();
        }
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_YFYJ_ZS_XSG.getVal())) {
            feeMoney33005 += settlementDetail.getOassMoney();
        }
    }
    long settlementMoney = feeMoney33021 - feeMoney33002 - feeMoney32003 + feeMoney32001 - feeMoney31001;
    long goodCommissionMoney = feeMoney33005 + feeMoney33002 + feeMoney32003 + feeMoney31001;
    settlementAmount.setSettlementAmount(settlementMoney);
    settlementAmount.setGoodsCommission(goodCommissionMoney);
    settlementAmount.setOrderId(orderMain.getOrderId());
    settlementAmount.setOrgCode(orderMain.getOrgCode());
    settlementAmount.setStationNo(String.valueOf(orderMain.getDeliveryStationNo()));
    settlementAmount.setBillTime(new Date());
    settlementAmount.setRetSuccess(false);
    return settlementAmount;
}

The logic of this method is relatively simple, that is, to assemble the OrderShoudSettlementAmount object. Two amounts need to be calculated, namely settlementMoney and goodCommissionMoney.

This requirement adds a fee item, and the method needs to be modified. The modified code looks like this:

private OrderShoudSettlementAmount getOrderShoudSettlementAmount(OrderDTO orderMain, List<SettlementDetail> details) {
    OrderShoudSettlementAmount settlementAmount = new OrderShoudSettlementAmount();
    // 应结金额=33021-33002-32003+32001-31001+34012-34013
    // 货款佣金=33005+33002+32003+31001+34013
    long feeMoney33021 = 0;
    long feeMoney33002 = 0;
    long feeMoney32003 = 0;
    long feeMoney32001 = 0;
    long feeMoney31001 = 0;
    long feeMoney33005 = 0;
    // 本次需求新增费项
    long feeMoney34012 = 0;
    // 本次需求新增费项
    long feeMoney34013 = 0;
    for (SettlementDetail settlementDetail : details) {
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_FREIGHT_ZS_NOSETTLE.getVal())) {
            feeMoney33021 += settlementDetail.getOassMoney();
        }
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_FREIGHT_YJ_ZS_NOSETTLE.getVal())) {
            feeMoney33002 += settlementDetail.getOassMoney();
        }
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_GOODS_YJ_WJTZ.getVal())) {
            feeMoney32003 += settlementDetail.getOassMoney();
        }
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_GOODS_NOSETTLE.getVal())) {
            feeMoney32001 += settlementDetail.getOassMoney();
        }
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_BDYJ_YJ_NOSETTLE.getVal())) {
            feeMoney31001 += settlementDetail.getOassMoney();
        }
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_YFYJ_ZS_XSG.getVal())) {
            feeMoney33005 += settlementDetail.getOassMoney();
        }
        // 本次需求新增费项
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_CHF_XSG_NOSETTLE.getVal())) {
            feeMoney34012 += settlementDetail.getOassMoney();
        }
        // 本次需求新增费项
        if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_CHF_YJ_XSG.getVal())) {
            feeMoney34013 += settlementDetail.getOassMoney();
        }
    }
    // 本次需求新增费项追加计算 + feeMoney34012 - feeMoney34013
    long settlementMoney = feeMoney33021 - feeMoney33002 - feeMoney32003 + feeMoney32001 - feeMoney31001 + feeMoney34012 - feeMoney34013;
    // 本次需求新增费项追加计算 + feeMoney34013
    long goodCommissionMoney = feeMoney33005 + feeMoney33002 + feeMoney32003 + feeMoney31001 + feeMoney34013;
    settlementAmount.setSettlementAmount(settlementMoney);
    settlementAmount.setGoodsCommission(goodCommissionMoney);
    settlementAmount.setOrderId(orderMain.getOrderId());
    settlementAmount.setOrgCode(orderMain.getOrgCode());
    settlementAmount.setStationNo(String.valueOf(orderMain.getDeliveryStationNo()));
    settlementAmount.setBillTime(new Date());
    settlementAmount.setRetSuccess(false);
    return settlementAmount;
}

2. Sniff out the bad smell of the code

In the book "Refactoring: Improving the Design of Existing Code", Martin Fowler lists 22 bad smells of code:

1.Duplicated Code 2.Long Method 3.Large Class 4.Long Parameter List 5.Divergent Change 6.Shotgun Surgery 7.Feature Envy 8.Data Clumps 9.Pri mitive Obsession (basic type paranoia) 10.Switch Statements (switch horror appearance) 11.Parallel Inheritance Hierarchies (parallel inheritance system) 12.Lazy Class (redundant class) 13.Speculative Generality (futuristic rhetoric) 14.Temporary Field (confusing temporary field) 15.Message Chains (over-coupled message chain) 16.Middle Man 17.Inappropriate Intimacy 18.Alternative Classes with Different Interfaces 19.Incomplete Library Class 20.Data Class 21.Refused Bequest 22.Comments Notes)

Referring to these 22 bad smells of code, I smelled 2 bad smells of code in the above method code:

Bad taste 1: Duplicated Code (duplicated code)

The accumulation operation of each fee item in the for loop is a repetitive code, and each time a new fee item is added, the repeated operation must be continuously increased.

for (SettlementDetail settlementDetail : details) {
    if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_FREIGHT_ZS_NOSETTLE.getVal())) {
        feeMoney33021 += settlementDetail.getOassMoney();
    }
    if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_FREIGHT_YJ_ZS_NOSETTLE.getVal())) {
        feeMoney33002 += settlementDetail.getOassMoney();
    }
    if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_GOODS_YJ_WJTZ.getVal())) {
        feeMoney32003 += settlementDetail.getOassMoney();
    }
    if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_GOODS_NOSETTLE.getVal())) {
        feeMoney32001 += settlementDetail.getOassMoney();
    }
    if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_BDYJ_YJ_NOSETTLE.getVal())) {
        feeMoney31001 += settlementDetail.getOassMoney();
    }
    if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_YFYJ_ZS_XSG.getVal())) {
        feeMoney33005 += settlementDetail.getOassMoney();
    }
    // 本次需求新增费项
    if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_CHF_XSG_NOSETTLE.getVal())) {
        feeMoney34012 += settlementDetail.getOassMoney();
    }
    // 本次需求新增费项
    if (settlementDetail.getExpenseType().equals(FeeInfoEnum.FEE_INFO_CHF_YJ_XSG.getVal())) {
        feeMoney34013 += settlementDetail.getOassMoney();
    }
}

Bad Taste 2: Divergent Change

Martin Fowler explains part of the bad smell in his book as follows:

We hope that software can be modified more easily-after all, software should be "soft" anyway. Once a change is needed, we want to be able to jump to a certain point in the system and make the change only there.

Now the method code is developed due to new requirements and has been modified in many places.

In fact, in addition to the bad smell of the above two codes, the biggest problem with the code of this method is that it is procedural-oriented rather than object-oriented.

Why do you say that?

As mentioned earlier, the main function of this method is to assemble the OrderShoudSettlementAmount object, so its logic should mainly reflect "assembly" rather than calculating the amount. The logic related to calculating the amount should be separated into a separate class, which not only conforms to the idea of ​​object-oriented programming, but also eliminates bad smells 2 .

3. Refactor the code

In response to the bad smell of the code sniffed out earlier, refactor decisively. After refactoring the code looks like this:

private OrderShoudSettlementAmount getOrderShoudSettlementAmount(OrderDTO orderMain, List<SettlementDetail> details) {
    OrderShoudSettlementAmount settlementAmount = new OrderShoudSettlementAmount();
    
    Map<Integer, Long> expenseTypeToFeeMoneyMap = Maps.newHashMap();
    for (SettlementDetail settlementDetail : details) {
        long feeMoney = Optional.ofNullable(expenseTypeToFeeMoneyMap.get(settlementDetail.getExpenseType())).orElse(0L);
        feeMoney += Optional.ofNullable(settlementDetail.getOassMoney()).orElse(0L);
        expenseTypeToFeeMoneyMap.put(settlementDetail.getExpenseType(), feeMoney);
    }
    long settlementMoney = SettlementMoneyCalcFeeInfoEnum.calcSettlementMoney(expenseTypeToFeeMoneyMap);
    long goodCommissionMoney = GoodCommissionMoneyCalcFeeInfoEnum.calcGoodCommissionMoney(expenseTypeToFeeMoneyMap);
    settlementAmount.setSettlementAmount(settlementMoney);
    settlementAmount.setGoodsCommission(goodCommissionMoney);
    settlementAmount.setOrderId(orderMain.getOrderId());
    settlementAmount.setOrgCode(orderMain.getOrgCode());
    settlementAmount.setStationNo(String.valueOf(orderMain.getDeliveryStationNo()));
    settlementAmount.setBillTime(new Date());
    settlementAmount.setRetSuccess(false);
    return settlementAmount;
}
enum SettlementMoneyCalcFeeInfoEnum {
    /**计算项*/
    FEE_33021(FeeInfoEnum.FEE_INFO_FREIGHT_ZS_NOSETTLE, "+"),
    FEE_33002(FeeInfoEnum.FEE_INFO_FREIGHT_YJ_ZS_NOSETTLE, "-"),
    FEE_32003(FeeInfoEnum.FEE_INFO_GOODS_YJ_WJTZ, "-"),
    FEE_32001(FeeInfoEnum.FEE_INFO_GOODS_NOSETTLE, "+"),
    FEE_31001(FeeInfoEnum.FEE_INFO_BDYJ_YJ_NOSETTLE, "-"),
    FEE_34012(FeeInfoEnum.FEE_INFO_CHF_XSG_NOSETTLE, "+"),
    FEE_34013(FeeInfoEnum.FEE_INFO_CHF_YJ_XSG, "-");

    private final FeeInfoEnum feeInfoEnum;
    private final String symbol;

    SettlementMoneyCalcFeeInfoEnum(FeeInfoEnum feeInfoEnum, String symbol) {
        this.feeInfoEnum = feeInfoEnum;
        this.symbol = symbol;
    }
    
    public static long calcSettlementMoney(Map<Integer, Long> expenseTypeToFeeMoneyMap) {
        // 应结金额=33021-33002-32003+32001-31001+34012-34013
        long settlementMoney = 0L;
        for (SettlementMoneyCalcFeeInfoEnum calcFeeInfoEnum : SettlementMoneyCalcFeeInfoEnum.values()) {
            if ("+".equals(calcFeeInfoEnum.symbol)) {
                settlementMoney += Optional
                        .ofNullable(expenseTypeToFeeMoneyMap.get(calcFeeInfoEnum.feeInfoEnum.getVal()))
                        .orElse(0L);
            }
            if ("-".equals(calcFeeInfoEnum.symbol)) {
                settlementMoney -= Optional
                        .ofNullable(expenseTypeToFeeMoneyMap.get(calcFeeInfoEnum.feeInfoEnum.getVal()))
                        .orElse(0L);
            }
        }
        return settlementMoney;
    }
}
enum GoodCommissionMoneyCalcFeeInfoEnum {
    /**计算项*/
    FEE_33005(FeeInfoEnum.FEE_INFO_YFYJ_ZS_XSG),
    FEE_33002(FeeInfoEnum.FEE_INFO_FREIGHT_YJ_ZS_NOSETTLE),
    FEE_32003(FeeInfoEnum.FEE_INFO_GOODS_YJ_WJTZ),
    FEE_31001(FeeInfoEnum.FEE_INFO_BDYJ_YJ_NOSETTLE),
    FEE_34013(FeeInfoEnum.FEE_INFO_CHF_YJ_XSG);

    private final FeeInfoEnum feeInfoEnum;

    GoodCommissionMoneyCalcFeeInfoEnum(FeeInfoEnum feeInfoEnum) {
        this.feeInfoEnum = feeInfoEnum;
    }

    public static long calcGoodCommissionMoney(Map<Integer, Long> expenseTypeToFeeMoneyMap) {
        // 货款佣金=33005+33002+32003+31001+34013
        long goodCommissionMoney = 0L;
        for (GoodCommissionMoneyCalcFeeInfoEnum calcFeeInfoEnum : GoodCommissionMoneyCalcFeeInfoEnum.values()) {
            goodCommissionMoney += Optional
                    .ofNullable(expenseTypeToFeeMoneyMap.get(calcFeeInfoEnum.feeInfoEnum.getVal()))
                    .orElse(0L);
        }
        return goodCommissionMoney;
    }
}

Four. Summary

The code of the above refactoring method is relatively simple. Some people may think that it is good without refactoring, and the readability of the code is not bad. Every modification is only a few places visible to the naked eye, so there is no need to spend time on it.

If you have the above thoughts, you may wish to understand the "broken window effect" in software engineering:

The broken window effect refers to the fact that in the software development process, if there is low-quality code or design, if it is not fixed in time, it will cause other developers to adopt the same low-quality solution. This can gradually escalate into more serious problems, causing the software system to become difficult to maintain, scale and improve. Therefore, in software development, it is very important to solve problems in a timely manner and maintain code quality, so as to avoid the negative impact of the broken window effect on the entire project.

Also take a look at Martin Fowler's partial explanation of refactoring in the book "Refactoring: Improving the Design of Existing Code":

Each step of refactoring is simple, even oversimplified: you just need to move a field from one class to another, pull some code out of one function to form another function, or push some code up and down the inheritance hierarchy. But collectively, these small modifications add up to radically improve the quality of the design.

Refactoring can not only improve code quality and make code elegant, but also allow us to apply what we have learned. The theoretical knowledge such as design ideas, principles, and patterns we have learned can often be truly practiced in refactoring.

Author: JD Retail Jia Wenxiong

Source: JD Cloud Developer Community

 

RustDesk 1.2: Using Flutter to rewrite the desktop version, supporting Wayland accused of deepin V23 successfully adapting to WSL 8 programming languages ​​​​with the most demand in 2023: PHP is strong, C/C++ demand slows down React is experiencing the moment of Angular.js? CentOS project claims to be "open to everyone" MySQL 8.1 and MySQL 8.0.34 are officially released Rust 1.71.0 stable version is released
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4090830/blog/10089382