代码重构之分解临时变量

Posted 编程随想曲

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码重构之分解临时变量相关的知识,希望对你有一定的参考价值。

意图

  • 如果临时变量承担多个责任,它就应该被替换(分解)为多个临时变量,每个变量只承担一个责任

示例

/**
 * Created by luo on 2017/4/24.
 */
public class SplitTemporaryVariableBefore {
    private double _height;
    private double _width;

    public void test(){
        double temp = 2 * (_height + _width);
        System.out.println(temp);
        temp = _height * _width;
        System.out.println(temp);
    }
}
/**
 * Created by luo on 2017/4/24.
 */
public class SplitTemporaryVariableAfter {
    private double _height;
    private double _width;

    public void test(){
        final double perimeter = 2 * (_height + _width);
        System.out.println(perimeter);
        final double area = _height * _width;
        System.out.println(area);
    }
}

 

以上是关于代码重构之分解临时变量的主要内容,如果未能解决你的问题,请参考以下文章