zoukankan      html  css  js  c++  java
  • 代码重构之引入解释性变量

    意图

    • 临时变量可以帮助你将表达式分解为比较容易管理的形式

    • 在较长的算法中,可以运用临时变量来解释每一步运算的意义

    示例

    /**
     * 引入解释性变量之前
     * Created by luo on 2017/4/23.
     */
    public class IntroduceExplainingVariableBefore {
        private String platform;
        private String browser;
        private int resize = 0;
    
        public void test(){
            if ((platform.toUpperCase().indexOf("MAC") > -1) && (browser.toUpperCase().indexOf("IE") > -1) && wasInitialized() && resize > 0){
                //do something
            }
        }
    
        private boolean wasInitialized() {
            return false;
        }
    }
    /**
     * 引入解释性变量之后
     * Created by luo on 2017/4/23.
     */
    public class IntroduceExplainingVariableAfter {
        private String platform;
        private String browser;
        private int resize = 0;
    
        public void test() {
            final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1;
            final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;
            final boolean wasResized = resize > 0;
    
            if (isMacOs && isIEBrowser && wasInitialized() && wasResized) {
                //do something
            }
        }
    
        private boolean wasInitialized() {
            return false;
        }
    }
  • 相关阅读:
    【中山纪念中学六年级模拟赛】方格翻转 题解
    高斯消元
    net 控件开发资料
    使用自定义验证组件库扩展 Windows 窗体
    POJ 3032
    UVa 10878 Decode the tape
    C语言I博客作业03
    第十周助教总结
    第十二周助教总结
    C语言I博客作业06
  • 原文地址:https://www.cnblogs.com/luoxiaolei/p/6759119.html
Copyright © 2011-2022 走看看