首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >代码重构之分解临时变量

代码重构之分解临时变量

作者头像
编程随想曲
发布于 2022-04-21 13:30:20
发布于 2022-04-21 13:30:20
4980
举报
文章被收录于专栏:编程随想曲编程随想曲

意图

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

示例

代码语言:javascript
复制
/**
 * 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);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-04-24,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档