码迷,mamicode.com
首页 > 其他好文 > 详细

代码重构之分解临时变量

时间:2017-04-24 22:56:49      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:before   代码   sys   重构   lis   creat   void   system   示例   

意图

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

示例

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

 

代码重构之分解临时变量

标签:before   代码   sys   重构   lis   creat   void   system   示例   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!