SlideShare une entreprise Scribd logo
1  sur  69
重构 刘发宾 2009-10-12
目录 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
设计是有限制的,未来不可预知 桥梁史上最有名的事故之一。 美国华盛顿州 Tacoma Narrows 桥倒塌,这次事故令桥梁的设计规范重新修改, 风力和共振分析成为桥梁设计必不可少的一部分, 重构可以带来简单的设计,同时不损失其灵活性,这也降低了设计过程的难度,减轻了设计的压力
设计是人类的活动 软件开发首先是一场沟通博弈,重构就是在代码写好之后改进它的设计 有了设计,我可以思考的更快,但是其中充满了小漏洞
管理软件的复杂度 程序的规模 程序的复杂度 程序员的工作就是保持程序的复杂度与规模的关系尽量接近线性函数,而不是指数函数,最小的复杂度,不要让无谓的复杂度快速的增长 亚里斯多德的” 事物的本质性质和附属性质”,本质复杂性解决问题核心。致力于本质复杂性,去除附复杂性。  1  噢卡姆剃刀原理:对一现象的好多种解释,最简单的往往是正确的,如无必要,务增复杂性。 2  迪米特法则 : 只跟最亲密的朋友讲话。任何对象不需要制定与之交互的那些对象的实现细节,减少复杂度 高内聚,松耦合
需求多变杂 小的改动不容忽视,层出不尽 封装,隔离变化
Double B  (bug bad smell) Refactoring  是一个  code review  和反馈的过程。在另一个时段重新审视自己或别人代码,可以更容易的发现问题和加深对代码的理解, Refactoring  是一个良好的软件开发习惯,问题越是发现在前端,软件修复以及维护成本会越低 ,问题越是发现在后端,成本越来越高
重构的效益 ,[object Object],[object Object],[object Object],叫我如何不喜欢你!!!! 脆弱 坚硬
何谓重构
概念的定义 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],概念——判断——逻辑,概念是抽象的结果,使得我们可以忽略细节,使得我们有限的心灵可以记忆管理更复杂更多的事情,定义了概念也方便我们交流沟通 概念还是要知道地!!!!
重构是一种方法论 牛顿万有引力定律无法解释水星运动的不规则形象,爱因斯坦构建的广义相对论理论即向下兼容了牛顿定律,又满足新的需求—自然的探索 数学系统的一直在重新构建,解决危机:有理数 ,无理数,实数,虚数, 欧几里德几何,笛卡尔的解析几何,黎曼几何,  乔治康拓的集合轮 重构也是我们认识世界,改造世界的方法论 .
重构和设计模式 ,[object Object],[object Object],[object Object]
何时重构 ? 重构本来就不是一件特别拨出时间做的事情,重构应该随时随地进行,你不应该为重构而重构,你之所以重构,是因为你想做别的事,而重构可以帮助你把那些事情做好。 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
代码的坏味道 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
测试 ,[object Object],[object Object],[object Object],[object Object]
原则 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],大自然优秀的面向对象设计师
如何重构 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
重新组织你的方法
Extract Method
Inline Method ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Int getRating(){ return (_numberOfLateDeliveries > 5) ? 2: 1 }
Replace Temp with Query
Introduce Explaining Variable
Split Temporary Variable
Remove Assignments to Parameters
Replace Method with Method Object
Substitute Algorithm
Remove Double Negative
Reduce Scope of Variable
Replace Iteration with Recursion
Replace Assignment with Initialization
Replace Static Variable with Parameter
Reverse Conditional
Split Loop
在对象之间搬移特性
Move method
Move Field
Extract Class
Inline Class
Hide Delegate
Remove Middle Man
Introduce Foreign Method
重新组织数据
Self Encapsulate Field
Replace Data Value with Object
[object Object],[object Object],[object Object],[object Object]
简化条件表达式
Decompose Conditional 分界表达式
Consolidate Conditional Expression 合并
Consolidate Duplicate Conditional Fragments
Replace Nested Conditional with Guard Clauses
Replace Conditional with Polymorphism 多态
Introduce Null Object
Introduce Assertion
简化函数调用 ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
处理概括关系
Pull Up Field
Pull Up Method
Pull Up Constructor Body
Push Down Method
Push Down Field
Extract Superclass
Extract Interface
Extract Subclass
Collapse Hierarchy 折叠继承体系
Form Template Method
Replace Inheritance/ Delegation with Delegation/Inheritance
大型重构 ,[object Object],[object Object],[object Object],[object Object]
总结 ,[object Object],[object Object]
评价 ,[object Object]

Contenu connexe

Tendances

Sql语句的优化
Sql语句的优化Sql语句的优化
Sql语句的优化abszhanghe
 
Beanstalk protocol
Beanstalk protocolBeanstalk protocol
Beanstalk protocolqianshi
 
深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)wang hongjiang
 
Java面试32题
Java面试32题Java面试32题
Java面试32题yiditushe
 
J2ee面试知识
J2ee面试知识J2ee面试知识
J2ee面试知识yiditushe
 
Struts Mitac(1)
Struts Mitac(1)Struts Mitac(1)
Struts Mitac(1)wangjiaz
 
Java多线程编程详解
Java多线程编程详解Java多线程编程详解
Java多线程编程详解yiditushe
 

Tendances (8)

Sql语句的优化
Sql语句的优化Sql语句的优化
Sql语句的优化
 
Beanstalk protocol
Beanstalk protocolBeanstalk protocol
Beanstalk protocol
 
深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)深入剖析Concurrent hashmap中的同步机制(上)
深入剖析Concurrent hashmap中的同步机制(上)
 
Java面试32题
Java面试32题Java面试32题
Java面试32题
 
jasmine入门指南
jasmine入门指南jasmine入门指南
jasmine入门指南
 
J2ee面试知识
J2ee面试知识J2ee面试知识
J2ee面试知识
 
Struts Mitac(1)
Struts Mitac(1)Struts Mitac(1)
Struts Mitac(1)
 
Java多线程编程详解
Java多线程编程详解Java多线程编程详解
Java多线程编程详解
 

En vedette

中山大学2010新生住宿指南
中山大学2010新生住宿指南中山大学2010新生住宿指南
中山大学2010新生住宿指南liufabin 66688
 
Mysql Replication Excerpt 5.1 En
Mysql Replication Excerpt 5.1 EnMysql Replication Excerpt 5.1 En
Mysql Replication Excerpt 5.1 Enliufabin 66688
 
北大2010新生住宿指南
北大2010新生住宿指南北大2010新生住宿指南
北大2010新生住宿指南liufabin 66688
 
Refactoring Simple Example
Refactoring Simple ExampleRefactoring Simple Example
Refactoring Simple Exampleliufabin 66688
 
Drupal Con My Sql Ha 2008 08 29
Drupal Con My Sql Ha 2008 08 29Drupal Con My Sql Ha 2008 08 29
Drupal Con My Sql Ha 2008 08 29liufabin 66688
 
北京师范大学2010新生住宿指南
北京师范大学2010新生住宿指南北京师范大学2010新生住宿指南
北京师范大学2010新生住宿指南liufabin 66688
 
Refactoring And Unit Testing
Refactoring And Unit TestingRefactoring And Unit Testing
Refactoring And Unit Testingliufabin 66688
 

En vedette (9)

Asml esitys geniem
Asml esitys geniemAsml esitys geniem
Asml esitys geniem
 
中山大学2010新生住宿指南
中山大学2010新生住宿指南中山大学2010新生住宿指南
中山大学2010新生住宿指南
 
Mysql Replication Excerpt 5.1 En
Mysql Replication Excerpt 5.1 EnMysql Replication Excerpt 5.1 En
Mysql Replication Excerpt 5.1 En
 
北大2010新生住宿指南
北大2010新生住宿指南北大2010新生住宿指南
北大2010新生住宿指南
 
Refactoring Simple Example
Refactoring Simple ExampleRefactoring Simple Example
Refactoring Simple Example
 
Drupal Con My Sql Ha 2008 08 29
Drupal Con My Sql Ha 2008 08 29Drupal Con My Sql Ha 2008 08 29
Drupal Con My Sql Ha 2008 08 29
 
北京师范大学2010新生住宿指南
北京师范大学2010新生住宿指南北京师范大学2010新生住宿指南
北京师范大学2010新生住宿指南
 
Refactoring And Unit Testing
Refactoring And Unit TestingRefactoring And Unit Testing
Refactoring And Unit Testing
 
Lecture28
Lecture28Lecture28
Lecture28
 

Similaire à Refactoring

Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试lydiafly
 
Frank‘s Learning Summary - Agile testing
Frank‘s Learning Summary - Agile testingFrank‘s Learning Summary - Agile testing
Frank‘s Learning Summary - Agile testingFrank Lee
 
重構—改善既有程式的設計(chapter 12,13)
重構—改善既有程式的設計(chapter 12,13)重構—改善既有程式的設計(chapter 12,13)
重構—改善既有程式的設計(chapter 12,13)Chris Huang
 
软件重构
软件重构软件重构
软件重构longhao
 
PHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsPHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsHo Kim
 
Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储 Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储 zhen chen
 
软件设计原则、模式与应用
软件设计原则、模式与应用软件设计原则、模式与应用
软件设计原则、模式与应用yiditushe
 
快速了解PostgreSQL
快速了解PostgreSQL快速了解PostgreSQL
快速了解PostgreSQL正中 周
 
常用JAVA代码质量静态检查工具
常用JAVA代码质量静态检查工具常用JAVA代码质量静态检查工具
常用JAVA代码质量静态检查工具dearhwj
 
IDDD Ch.09 Module
IDDD Ch.09 ModuleIDDD Ch.09 Module
IDDD Ch.09 ModuleEason Kuo
 
Refactoring with Patterns in PHP
Refactoring with Patterns in PHPRefactoring with Patterns in PHP
Refactoring with Patterns in PHPJace Ju
 
5, initialization & cleanup
5, initialization & cleanup5, initialization & cleanup
5, initialization & cleanupted-xu
 
软件工程
软件工程软件工程
软件工程bill0077
 
網站設計100步
網站設計100步網站設計100步
網站設計100步evercislide
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introductionotakustay
 

Similaire à Refactoring (20)

SCJP ch09
SCJP ch09SCJP ch09
SCJP ch09
 
Ecma script edition5-小试
Ecma script edition5-小试Ecma script edition5-小试
Ecma script edition5-小试
 
Frank‘s Learning Summary - Agile testing
Frank‘s Learning Summary - Agile testingFrank‘s Learning Summary - Agile testing
Frank‘s Learning Summary - Agile testing
 
重構—改善既有程式的設計(chapter 12,13)
重構—改善既有程式的設計(chapter 12,13)重構—改善既有程式的設計(chapter 12,13)
重構—改善既有程式的設計(chapter 12,13)
 
软件重构
软件重构软件重构
软件重构
 
PHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsPHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming Skills
 
前端测试
前端测试前端测试
前端测试
 
前端测试
前端测试前端测试
前端测试
 
Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储 Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储
 
软件设计原则、模式与应用
软件设计原则、模式与应用软件设计原则、模式与应用
软件设计原则、模式与应用
 
快速了解PostgreSQL
快速了解PostgreSQL快速了解PostgreSQL
快速了解PostgreSQL
 
常用JAVA代码质量静态检查工具
常用JAVA代码质量静态检查工具常用JAVA代码质量静态检查工具
常用JAVA代码质量静态检查工具
 
Xpp
XppXpp
Xpp
 
IDDD Ch.09 Module
IDDD Ch.09 ModuleIDDD Ch.09 Module
IDDD Ch.09 Module
 
Refactoring with Patterns in PHP
Refactoring with Patterns in PHPRefactoring with Patterns in PHP
Refactoring with Patterns in PHP
 
5, initialization & cleanup
5, initialization & cleanup5, initialization & cleanup
5, initialization & cleanup
 
软件工程
软件工程软件工程
软件工程
 
網站設計100步
網站設計100步網站設計100步
網站設計100步
 
ES5 introduction
ES5 introductionES5 introduction
ES5 introduction
 
Code guidelines
Code guidelinesCode guidelines
Code guidelines
 

Plus de liufabin 66688

人大2010新生住宿指南
人大2010新生住宿指南人大2010新生住宿指南
人大2010新生住宿指南liufabin 66688
 
武汉大学2010新生住宿指南
武汉大学2010新生住宿指南武汉大学2010新生住宿指南
武汉大学2010新生住宿指南liufabin 66688
 
天津大学2010新生住宿指南
天津大学2010新生住宿指南天津大学2010新生住宿指南
天津大学2010新生住宿指南liufabin 66688
 
清华2010新生住宿指南
清华2010新生住宿指南清华2010新生住宿指南
清华2010新生住宿指南liufabin 66688
 
南京大学2010新生住宿指南
南京大学2010新生住宿指南南京大学2010新生住宿指南
南京大学2010新生住宿指南liufabin 66688
 
复旦2010新生住宿指南
复旦2010新生住宿指南复旦2010新生住宿指南
复旦2010新生住宿指南liufabin 66688
 
Application Partitioning Wp
Application Partitioning WpApplication Partitioning Wp
Application Partitioning Wpliufabin 66688
 
090507.New Replication Features(2)
090507.New Replication Features(2)090507.New Replication Features(2)
090507.New Replication Features(2)liufabin 66688
 
High Performance Mysql
High Performance MysqlHigh Performance Mysql
High Performance Mysqlliufabin 66688
 
high performance mysql
high performance mysqlhigh performance mysql
high performance mysqlliufabin 66688
 

Plus de liufabin 66688 (13)

人大2010新生住宿指南
人大2010新生住宿指南人大2010新生住宿指南
人大2010新生住宿指南
 
武汉大学2010新生住宿指南
武汉大学2010新生住宿指南武汉大学2010新生住宿指南
武汉大学2010新生住宿指南
 
天津大学2010新生住宿指南
天津大学2010新生住宿指南天津大学2010新生住宿指南
天津大学2010新生住宿指南
 
清华2010新生住宿指南
清华2010新生住宿指南清华2010新生住宿指南
清华2010新生住宿指南
 
南京大学2010新生住宿指南
南京大学2010新生住宿指南南京大学2010新生住宿指南
南京大学2010新生住宿指南
 
复旦2010新生住宿指南
复旦2010新生住宿指南复旦2010新生住宿指南
复旦2010新生住宿指南
 
Optimzing mysql
Optimzing mysqlOptimzing mysql
Optimzing mysql
 
Application Partitioning Wp
Application Partitioning WpApplication Partitioning Wp
Application Partitioning Wp
 
090507.New Replication Features(2)
090507.New Replication Features(2)090507.New Replication Features(2)
090507.New Replication Features(2)
 
Mysql Replication
Mysql ReplicationMysql Replication
Mysql Replication
 
High Performance Mysql
High Performance MysqlHigh Performance Mysql
High Performance Mysql
 
high performance mysql
high performance mysqlhigh performance mysql
high performance mysql
 
Refactoring Example
Refactoring ExampleRefactoring Example
Refactoring Example
 

Refactoring