代码重构之移除对参数的赋值

意图

  • 区别按值传递和按引用传递,提升代码的清晰度

  • 如果只以参数表示被传递进来的东西,代码会清晰很多

示例

/**
 * 移除对参数的赋值之前
 * Created by luo on 2017/4/25.
 */
public class RemoveAssignmentsToParametersBefore {
    void discount (int inputVal, int quantity, int yearToDate){
        if (inputVal > 50){
            inputVal -= 20;
        }
    }
}
/**
 * 移除对参数的赋值之后
 * Created by luo on 2017/4/25.
 */
public class RemoveAssignmentsToParametersAfter {
    void discount (int inputVal, int quantity, int yearToDate){
        int result = inputVal;
        if (inputVal > 50){
            result -= 20;
        }
    }
}
原文地址:https://www.cnblogs.com/luoxiaolei/p/6765032.html