• 重构之重新组织函数(Remove Assignments to Parameters)


    public class RemoveAssignmentsToParameters {
    
        //before remove assigments to parameters
        public int discount(int inputVal,int quantity,int yearToDate)
        {
            if(inputVal>50)
                inputVal-=2;
            if(inputVal>100)
                inputVal-=1;
            if(inputVal>10000)
                inputVal-=4;
            return inputVal;
        }
    
        //after remove assigments to parameters
        public int discountAdvanced(int inputVal,int quantity,int yearToDate)
        {
            int result = inputVal;
            if(inputVal>50)
                result-=2;
            if(inputVal>100)
                result-=1;
            if(inputVal>10000)
                result-=4;
    
            return result;
    
        }
    
    }

    void Change(Ojbect foo){
       foo.modifySomeWay();   //  that's  ok
       foo=  anotherOjbect;  //这种写法,就是对参数赋值,最好不要这么写.要移去.
    }

         

        动机:

                    要清楚“对参数赋值”这个说法的意思。如果你把一个名为foo的对象作为参数传给某个函数,那么“对参数赋值”意味着改变foo,使它引用另外一个对象。如果你在“被传入对象”身上进行什么操作,那没什么问题。这里只针对“foo被改而指向另一个对象”这种情况来讨论。 

           做法:

           1、建立一个临时变量,把待处理的参数值赋予它。

           2、以“对参数的赋值”为界,将其后所有对此参数的引用点,全部替换为“对此临时变量的引用”。

           3、修改赋值语句,使其改为对新建之临时变量赋值。

           4、编译、测试。如果代码的语义是按引用传递的,请在调用端检查调用后是否还使用了这个参数,也要检查有多少个按引用传递的参数被赋值后又被调用。请尽量只以rteturn方式返回一个值。如果需要发挥的值不止一个,看看可否把需返回的大堆数据变成单一对象,或干脆为每个返回值设计对应的一个单独函数。

  • 相关阅读:
    【QTP小技巧】02_QTP中Complete Word 实现(转载)
    【QTP专题】04_对象及操作方法
    【QTP专题】03_Add-in Manager插件
    【QTP专题】02_时间同步点问题
    loadrunner 11问题汇总
    system idle process
    html div四边阴影效果
    通过CSS实现 文字渐变色 的两种方式
    UIgradients – 美丽的UI渐变色分享站 并可转成CSS代码
    display:inline-block带来的问题及解决办法
  • 原文地址:https://www.cnblogs.com/newbee0101/p/11970707.html
Copyright © 2020-2023  润新知