首页 > 代码库 > 代码重构之分解临时变量

代码重构之分解临时变量

意图

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

示例

/** * 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);    }}

 

代码重构之分解临时变量