改善既有的代码-重构.ppt_第1页
改善既有的代码-重构.ppt_第2页
改善既有的代码-重构.ppt_第3页
改善既有的代码-重构.ppt_第4页
改善既有的代码-重构.ppt_第5页
已阅读5页,还剩37页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、重构技术,邱百超 badqiu(a),讲解内容,重构介绍 重构原则 代码的坏味道 重构技巧 重构工具使用实例 Q if( name != null if( age != null if( !isNullOrEmpty( name ) ) . String age = request.getParameter(Age); if( !isNullOrEmpty( age ) ) . private boolean isNullOrEmpty( final String string ) if( string != null ,Extract Methods(提炼函数:清除注释),void prin

2、tOwing() /print banner System.out.println(“*”); System.out.println(“Banner”); System.out.println(“*”); /print details System.out.println (name: + _name); System.out.println (amount + getOutstanding(); ,void printOwing() printBanner(); printDetails(getOutstanding(); void printBanner() System.out.prin

3、tln(“*”); System.out.println(“Banner”); System.out.println(“*”); void printDetails (double outstanding) System.out.println (name: + _name); System.out.println (amount + outstanding); ,Inline Method(将函数内联化),Inline Temp(将临时变量内联化),Replace Temp with Query(以查询取代临时变量),double basePrice = _quantity * _itemP

4、rice; if (basePrice 1000) return basePrice * 0.95; else return basePrice * 0.98;,if (basePrice() 1000) return basePrice() * 0.95; else return basePrice() * 0.98; . double basePrice() return _quantity * _itemPrice; ,Introduce Explaining Variable(引入解释性变量),boolean isMacOs = platform.toUpperCase().index

5、Of(MAC) -1; boolean isIEBrowser = browser.toUpperCase().indexOf(IE) -1; boolean wasResized = resize 0; if (isMacOs System.out.println (temp); temp = _height * _width; System.out.println (temp);,double perimeter = 2 * (_height + _width); System.out.println (perimeter); double area = _height * _width;

6、 System.out.println (area);,Remove Assignments to Parameters(移除对参数的赋值),Replace Method with Method Object(以函数对象取代函数),Substitute Algorithm(替换算法),Replace Nested Conditional with Guard Clauses,double getPayAmount() double result; if (_isDead) result = deadAmount(); else if (_isSeparated) result = separa

7、tedAmount(); else if (_isRetired) result = retiredAmount(); else result = normalPayAmount(); ; return result; ;,double getPayAmount() if (_isDead) return deadAmount(); if (_isSeparated) return separatedAmount(); if (_isRetired) return retiredAmount(); return normalPayAmount(); ;,Split Loop - 1,void

8、printValues() double averageAge = 0; double totalSalary = 0; for (int i = 0; i people.length; i+) averageAge += peoplei.age; totalSalary += peoplei.salary; averageAge = averageAge / people.length; System.out.println(averageAge); System.out.println(totalSalary); ,void printValues() double totalSalary

9、 = 0; for (int i = 0; i people.length; i+) totalSalary += peoplei.salary; double averageAge = 0; for (int i = 0; i people.length; i+) averageAge += peoplei.age; averageAge = averageAge / people.length; System.out.println(averageAge); System.out.println(totalSalary); ,Split Loop - 2,void printValues(

10、) System.out.println(averageAge(); System.out.println(totalSalary(); private double averageAge() double result = 0; for (int i = 0; i people.length; i+) result += peoplei.age; return result / people.length; private double totalSalary() double result = 0; for (int i = 0; i people.length; i+) result +

11、= peoplei.salary; return result; ,简化条件表达式,分解表达式,if(date.before(SUMMER_START) | date.after(SUMMER_END) charge = quantity * _winterRate + _winterServiceCharge; else charge = quantity * _summerRate,if(notSummer(date) charge = winterCharge(quantity); else charge = summerCharge(quantity);,Remove Double Negative,if ( !item.isNotFound() ) if ( item.isFound() ),在对象之间搬移特性,搬移函数(Move Method) 搬移值域(Move Field) 提炼类(Extract Class) 将类内联化(Inline Class) 隐藏委托关系(Hide Delegate) 移除中间人(Remove Middle Man) 引入外加函数(Introduce Foreign Met

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论