首页 > 其他 > 详细

代码重构之分解临时变量

时间:2017-04-24 22:13:02      阅读:245      评论:0      收藏:0      [点我收藏+]

意图

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

示例

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

 

代码重构之分解临时变量

原文:http://www.cnblogs.com/luoxiaolei/p/6759129.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!