软件重构最新课件.ppt
《软件重构最新课件.ppt》由会员分享,可在线阅读,更多相关《软件重构最新课件.ppt(102页珍藏版)》请在三一办公上搜索。
1、浅谈软件重构与性能优化,参考书目,?,?,?,重构,:改善既有代码的设计,作,译,者:,MARTIN FOWLER,者:侯捷,熊节,什么是重构?,?,重构(,Refactoring,):在不改变软件的,功能和外部可见性的情况下,为了改善,软件的结构,提高清晰性、可扩展性和,可重用性而对软件进行的改造,对代码,内部的结构进行优化。,2008-04-02,为什么要重构,?,?,?,?,?,改进软件的设计,提高代码质量,可维护性,Refactoring,帮助尽早的发现错误,(,Defects,),Refactoring,可以提高提高开发速度,2008-04-02,什么时候需要重构,?,?,?,?,在
2、开始增加一个新的功能之前,在修复一个错误的时候,在做,Code Review,的时候,2008-04-02,何时不应该重构,?,?,?,?,代码太混乱,设计完全错误,明天是,DeadLine,重构的工作量显著的影响,Estimate,2008-04-02,重构流程,?,?,?,读懂代码(包括测试例子代码),进行重构,运行所有的,Unit Tests,2008-04-02,重构与设计,?,重构与设计是互补的,程序应该是先设计,而在开始编码后,设计上的不足可以用重,构来弥补,.,设计应该是适度的设计,而不,必过度的设计,.,如果能很容易的通过重构,来适应需求的变化,那么就不必过度的设,计,当需求改
3、变时再重构代码,。,2008-04-02,重构与性能,?,?,?,时间预算法,持续关注法,良好的分解方式,2008-04-02,时间预算法,?,在设计时就对程序花费的时间进行预算,通常用于性能要求极高的实时系统,.,普通,的企业应用程序一般对性能要求不高,.,只,要不太慢就可以了,。,2008-04-02,持续关注法,?,要求程序员在任何时间都要设法保持系,统的高性能,.,这个方法有个缺陷,就是大,部分的程序,90%,的优化工作都是白费劲,这样会浪费大量的时间,。,2008-04-02,良好的分解方式,?,这个方式是在开发程序阶段不对性能投,以任何关注,直到进入性能优化阶段,再,分析程序中性能
4、差的程序,然后对这些程,序进分解,查出性能差的程序,进行优化。,2008-04-02,代码编写的,22,宗罪,(,Bad,Smells,in,Code,),?,?,?,?,?,?,?,?,?,?,?,(,1,),Duplicated,Code,(重复的代码),(,2,),Long,Method,(过长函数),(,3,),Large,Class,(过大类),(,4,),Long,Parameter,List,(过长参数列),(,5,),Divergent,Change,(发散式变化),(,6,),Shortgun,Surgery,(霰弹式修改),(,7,),Feature,Envy,(依恋情结)
5、,(,8,),Data,Clumps,(数据泥团),(,9,),Primitive,Obsession,(基本型别偏执),(,10,),Switch,Statements,(,switch,惊悚现身),(,11,),Parallel,Inheritance,Hierarchies,(平行继承体系),2008-04-02,?,(,12,),Lazy,Class,(冗赘类),?,(,13,),Speculative,Generality,(夸夸其谈未来性),?,(,14,),Temporary,Field,(令人迷惑的暂时值域),?,(,15,),Message,Chains,(过度耦合的消息链)
6、,?,(,16,),Middle,Man,(中间转手人),?,(,17,),Inappropriate,Intimacy,(狎昵关系),?,(,18,),Alternative,Classes,with,Different,Interfaces,同工的类),?,(,19,),Incomplete,Library,Class,(不完善的程序库类),?,(,20,),Data,Class,(纯稚的数据类),?,(,21,),Refused,Bequest,(被拒绝的遗赠),?,(,22,),Comments,(过多的注释),2008-04-02,(异曲,建立测试体系,(,Building,Test
7、s,),?,?,?,(,1,)自我测试码(,Self-testing,Code,)的价值,(,2,)可以参考,JUnit,测试框架(,Testing,Framework,),(,3,)添加更多测试,2008-04-02,重新组织你的函数,(,Composing,Methods,),?,?,?,?,?,?,?,?,?,(,1,),Extract,Method,(提炼函数),(,2,),Inline,Method,(将函数内联化),(,3,),Inline,Temp,(将临时变量内联化),(,4,),Replace,Temp,With,Query,(以查询取代临时变量),(,5,),Introdu
8、ce,Explaining,Variable,(引入解释性变量),(,6,),Split,Temporary,Variable,(剖解临时变量),(,7,),Remove,Assignments,to,Parameters,(移除对参数的赋值动,作),(,8,),Replace,Method,with,Method,Object,(以函数对象取代函数),(,9,),Substitute,Algorithm,(替换你的算法),2008-04-02,Extract,Method,(提炼函数),void printOwing() ,/print banner,System.out.println(,
9、“,*,”,);,System.out.println(,“,Banner,”,);,System.out.println(,“,*,”,);,/print details,System.out.println (name: + _name);,System.out.println (amount +,getOutstanding();,void printOwing(),printBanner();,printDetails(getOutstanding();,V,oid printBanner(),/print banner,System.out.println(“*”);,System.
10、out.println(“Banner”);,System.out.println(“*”);,void printDetails (double outstanding),System.out.println (name: + _name);,System.out.println (amount + outstanding);,2008-04-02,Inline,Method,(将函数内联化),int getRating() ,return (moreThanFiveLateDeliveries() ? 2 : 1; ,boolean moreThanFiveLateDeliveries()
11、 ,return _numberOfLateDeliveries 5;,int getRating() ,return (_numberOfLateDeliveries 5) ? 2 : 1;,2008-04-02,Inline,Temp,(将临时变量内联化),double basePrice = anOrder.basePrice();,return (basePrice 1000) ;,return (anOrder.basePrice() 1000) ;,2008-04-02,Replace,Temp,With,Query,(以查询取代临时变量),double basePrice = _
12、quantity * _itemPrice;,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; ,2008-04-02,Introduce,Explaining,Variable,(引入解释性变量),if ( (platform.toUp
13、perCase().indexOf(MAC) -1),& (browser.toUpperCase().indexOf(IE) -,1),& wasInitialized(),& resize 0 ), / do something ,final boolean isMacOs = atform.toUpperCase().indexOf(MAC) -1;,final boolean isIEBrowser = browser.toUpperCase().indexOf(IE) -1;,final boolean wasResized = resize 0;,if (isMacOs & isI
14、EBrowser & wasInitialized() & wasResized) / do something ,2008-04-02,Remove,Assignments,to,Parameters,(移除对参数的赋值动作),int discount (int inputVal, int quantity, int yearToDate),if (inputVal 50) inputVal -= 2;,int discount (int inputVal, int quantity, int yearToDate) ,int result = inputVal; if (inputVal
15、50) result -= 2;,如果参数是,Object,容易误赋值。采用,final,来防止误用参数,2008-04-02,Replace,Method,with,Method,Object,(以函数对象取代函数),class Order.,double price() ,double primaryBasePrice;,double secondaryBasePrice;,double tertiaryBasePrice;,/ long computation; .,或者可以采用,static method,2008-04-02,Substitute,Algorithm,(替换你的算法)
16、,String foundPerson(String people),for (int i = 0; i people.length; i+) ,if (peoplei.equals (Don),return Don;,if (peoplei.equals (John) ,return John;,if (peoplei.equals (Kent),return Kent;,return ;,String foundPerson(String people),List candidates,= Arrays.asList(new String,Don,John, Kent);,for (int
17、 i=0; ipeople.length; i+),if (candidates.contains(peoplei),return peoplei; return ;,2008-04-02,在对象之间移动特性,(,Moving,Features,Between,Objects,),?,?,?,?,?,?,?,?,(,1,),Move,Method,(搬移函数),(,2,),Move,Field,(搬移值域),(,3,),Extract,Class,(提炼类),(,4,),Inline,Class,(将类内联化),(,5,),Hide,Delegate,(隐藏委托关系),(,6,),Remove
18、,Middle,Man,(移除中间人),(,7,),Introduce,Foreign,Method,(引入外加函数),(,8,),Introduce,Local,Extension,(引入本地扩展),2008-04-02,Move,Method,(搬移函数),2008-04-02,Move,Field,(搬移值域),2008-04-02,Extract,Class,(提炼类),2008-04-02,Inline,Class,(将类内联化),2008-04-02,Hide,Delegate,(隐藏委托关系),2008-04-02,Remove,Middle,Man,(移除中间人),2008-0
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 软件 最新 课件
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-1848103.html