Refactoring is about improving the design of existing code. It is the process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal structure. With refactoring you can even take a bad design and rework it into a good one. This book offers a thorough discussion of the principles of refactoring, including w...
Refactoring is about improving the design of existing code. It is the process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal structure. With refactoring you can even take a bad design and rework it into a good one. This book offers a thorough discussion of the principles of refactoring, including where to spot opportunities for refactoring, and how to set up the required tests. There is also a catalog of more than 40 proven refactorings with details as to when and why to use the refactoring, step by step instructions for implementing it, and an example illustrating how it works The book is written using Java as its principle language, but the ideas are applicable to any OO language.
作者简介
· · · · · ·
Martin Fowler 世界软件开发大师,在面向对象分析设计、UML、模式、XP和重构等领域都有卓越贡献,现为著名软件开发咨询公司ThoughtWorks的首席科学家。他的多部著作《分析模式》、《UML精粹》和《企业应用架构模式》等都已经成为脍炙人口的经典。
其他参编者——
Kent Beck 软件开发方法学的泰斗,极限编程的创始人。他是Three Rivers Institute公司总裁,也是Agitar Software的成员。
John Brant和Don Roberts The Refactory公司的创始人,Refactoring Browser (http://st-www.cs.illinois.edu/users/brant/Refactory/)的开发者,多年来一直从事研究重构的实践与理论。
refactoring does not change the observable behavior of the software
Why Should You Refactor?
Refactoring Improves the Design of Software
Refactoring Makes Software Easier to Understand
Refactoring Helps You Find Bugs
Refactoring Helps You Program Faster
即使在开发过程中,当你发现重复或相似的代码时,也应该立刻重构;当变化发生时,如果该变化影响不止一处,重构就应该粉墨登场。经常的重构可以保证代码常拭常新,如利刃一般锋利。“不要容忍破窗户”
如果两个或更多的地方实现同一职责,则改变时会带来麻烦。所以要遵循DRY原则,单一职责。
When Should You Refactor?
The Rule of Three(Three strikes and you refactor)
Refactor When You Add Function
Refactor When You Need to Fix a Bug
Refactor As You Do a Code Review
The most common time to refactor is when I want to add a new feature to some software.
Reasons:1.refactoring helps me understand some code I need to modify.2.another driver of refactoring is a design that does not help me add a fe... (查看原文)
For much of the history of software development, most people believed that we design first, and only when done with design should we code. Over time, the code will be modified, and the integrity of th...For much of the history of software development, most people believed that we design first, and only when done with design should we code. Over time, the code will be modified, and the integrity of the system—its structure according to that design—gradually fades. The code slowly sinks from engineering to hacking.(展开)
A list of signals of refactoring. This chapter is kind of abstract. It would be better to go back to the chapter after reading the rest concrete examples.
2014-12-21 15:50:46
A list of signals of refactoring. This chapter is kind of abstract. It would be better to go back to the chapter after reading the rest concrete examples.
Good example - Exact method - Test - Rename - Test - Move method (if necessary) and leave the old method to delegate to the new method - Test - Change all the callsites - Test - Remove old method - Test - Replace Temp with Query (with the cost of performance?) Replace Type with State/Strategy - Self encapsulate field: all uses of Type go through getting and setting methods - Class Movie { getCh...
2014-12-15 11:33:03
Good example
- Exact method
- Test
- Rename
- Test
- Move method (if necessary) and leave the old method to delegate to the new method
- Test
- Change all the callsites
- Test
- Remove old method
- Test
- Replace Temp with Query (with the cost of performance?)
Replace Type with State/Strategy
- Self encapsulate field: all uses of Type go through getting and setting methods
-
Class Movie
{
getCharge() {
switch (getPriceCode())
{
Case 1:
Case 2:
...
}
}
}
=>
Create
New Class Price
and
Subclasses of Price: RegularPrice, ChildPrice, etc
Class Movie
{
setPriceCode(int arg) {
switch (arg) {
Case 1: _price = new RegularPrice();
Case 2: ...
}
}
getCharge() {
return _price.getCharge();
}
}
Private Price _price;
Tips:
Rename variables for clarification
Change the program in small step
Duplicated Code the same expression in two methods of the same class. the same expression in two sibling subclasses. duplicated code in two unrelated classes. Long Method The longer a procedure is, the more difficult it is to understand. Development environments that allow you to see two methods at once help to eliminate this step, but the real key to making it easy to understand small methods...
2013-06-21 17:23:22
Duplicated Code
the same expression in two methods of the same class.
the same expression in two sibling subclasses.
duplicated code in two unrelated classes.
Long Method
The longer a procedure is, the more difficult it is to understand.
Development environments that allow you to see two methods at once help to eliminate this step, but the real key to making it easy to understand small methods is good naming. If you have a good name for a method you don't need to look at the body.
A method with lots of parameters and temporary variables, these elements get in the way of extracting methods.
Large Class
When a class is trying to do too much, it often shows up as too many instance variables. When a class has too many instance variables, duplicated code cannot be far behind.
Long Parameter List
Divergent Change(发散式变化)
When you find you have to add a feature to a program, and the program's code is not structured in a convenient way to add the feature, first refactor the program to make it easy to add the feature, then add the feature. 当你要为你的程序添加一个功能特性时,发现现有的代码结构不利于添加此功能特性时,首先要重构程序使得程序本身变得易于添加加功能特性,然后再添加这个功能特性。 The First Step in Refact...
2013-05-24 10:36:59
When you find you have to add a feature to a program, and the program's code is not structured in a convenient way to add the feature, first refactor the program to make it easy to add the feature, then add the feature.
当你要为你的程序添加一个功能特性时,发现现有的代码结构不利于添加此功能特性时,首先要重构程序使得程序本身变得易于添加加功能特性,然后再添加这个功能特性。
The First Step in Refactoring
The tests are essential because even though I follow refactorings structured to avoid most of the opportunities for introducing bugs, I'm still human and still make mistakes.
做好测试的案例。测试案例非常重要,尽管我已十分谨慎的根据重构结构来避免各种产生Bug的可能,但仍有可能出现各种错误。
It is essential for refactoring that you have good tests. It's worth spending the time to build the tests, because the tests give you the security you need to change the program later. It is vital to make tests self-checking.
有好的测试案例对重构来讲非常重要。花时间去创建这些测试也是非常值得的,因为这些测试给你接下的更改代码做了一个正确的保证。测试案例能自我检查非常关键。
Refactoring changes the programs in small steps. If you make a mistake, it is easy to find the bug.
重构的时候要一小步一小步的修改程序。如果出现错误,也能快速容易的发现错误。
Is renaming worth the effort? Absolutely. Good code should communicate what it is doing clearly, and variable names are a key to clear code. Never be afraid to change the names of things to improve clarity. With good find and replace tools, it is usually not difficult. Strong typing and testing will highlight anything you miss. Remember
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
有必要重命名吗?完全有。好的代码应该与它做的事情清晰的连接起来,而且变量名称是清晰代码的关键。为了更清晰,永远不要害怕去改变它们的名称。使用一个好的查找与替换工具,这个工作通常不那么困难。强类型和检测能高亮显示你丢失的信息。要记住:
“傻子可以写出计算机理解的代码,但好的程序员能写出人能理解的代码。”
重构就是节奏就是:测试,改动,测试,改动,再测试,再改动。这样的节奏能保证重构能够迅速,安全的开展。
重构的定义: 名词定义: Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior. 是对软件内部架构的一种改变,这种改变能在不改变软件表现形式的前提下让软件结构更易于理解和修改。 动词定义: Refactor (verb): to restructure software by applying a series of refactorings without changing its...
2013-05-24 14:28:55
重构的定义:
名词定义:
Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.
是对软件内部架构的一种改变,这种改变能在不改变软件表现形式的前提下让软件结构更易于理解和修改。
动词定义:
Refactor (verb): to restructure software by applying a series of refactorings without changing its observable behavior.
在不改变软件的表现形式的前提下运用一系列重构方法对软件进行重构。
First, the purpose of refactoring is to make the software easier to understand and modify. Only changes made to make the software easier to understand are refactorings. A good contrast is performance optimization.However, the purpose is different. Performance optimization often makes code harder to understand, but you need to do it to get the performance you need.
首先,重构的目的是让软件程序更易于理解和修改。只是让代码更易于理解的修改就是重构,与之相反的就是性能优化。当然彼此的目的是不一样的,性能优化往往会让代码更难理解,但是能让你把软件性能提高到你想要的位置。
The second thing I want to highlight is that refactoring does not change the observable behavior of the software. The software still carries out the same function that it did before. Any user, whether an end user or another programmer, cannot tell that things have changed.
其二,要强调的是重构不会改变软件的外在表现。重构前后,软件执行相同的功能。任何人,无论是终端还是其他程序员,不能告诉我东西不一样了。
The Two Hats
When you use refactoring to develop software, you divide your time between two distinct activities: adding function and refactoring. When you add function, you shouldn't be changing existing code; you are just adding new capabilities. You can measure your progress by adding tests and getting the tests to work. When you refactor, you make a point of not adding function; you only restructure the code. You don't add any tests (unless you find a case you missed earlier); you only change tests when you absolutely need to in order to cope with a change in an interface. As you develop software, you probably find yourself swapping hats frequently.
在开发软件的时候使用重构时,实际上你是在把时间分成两部分做两个活动:增加功能和进行重构。当你增加功能的时候,你不需要修改现有的代码,只是增加一个新功能而已,可以通过增加测试,让测试通过来检测你的进度;当你进行重构时,不是在增加功能的那个点,只是重构你的代码而已,你不需要增加测试(除非你发现之前有遗漏),或只是为了因为不得不改变的接口而改变你的测试。当你在开发的时候,就会发现自己在频繁的切换这两种活动。
为什么要重构(Why Should You Refactor? )
Refactoring Improves the Design of Software
重构能改善软件的设计
Without refactoring, the design of the program will decay. Poorly designed code usually takes more code to do the same things, often because the code quite literally does the same thing in several places. Thus an important aspect of improving design is to eliminate duplicate code. Reducing the amount of code won't make the system run any faster, because the effect on the footprint of the programs rarely is significant. Reducing the amount of code does, however,make a big difference in modification of the code.The more code there is, the harder it is to modify correctly.
如果没有重构,程序的设计将会被搁置。不好的代码设计会用多余的代码去做同样的事情,往往是因为代码在不同地方做相同的事情。因此,改善设计很重要的一个方面就是删除重复的代码。减少代码量不会让程序跑得更快,因为程序运行的轨迹的影响并不很重要。但是减少代码量,对修改代码来说有很大的不同。代码越多,越不容易正确修改。
Refactoring Makes Software Easier to Understand
重构能让软件更异于理解
重构能让他人更快速的理解你的代码,也方便你今后查询你的代码。重构也能帮助自己理解不熟悉的代码。
Refactoring Helps You Find Bugs
重构能帮助你找出错误
Kent Beck:"I'm not a great programmer; I'm just a good programmer with great habits." Refactoring helps me be much more effective at writing robust code.
“我不是个伟大的程序员,我只是一个有着良好习惯的好程序员。”重构能让你更高效的写出健壮的代码。
Refactoring Helps You Program Faster
重构能让你更快的编码
In the end, all the earlier points come down to this: Refactoring helps you develop code more quickly.
最后,所有前面的观点都指向这个:重构能让你更快的编码。
This sounds counterintuitive. When I talk about refactoring, people can easily see that it improves quality. Improving design, improving readability, reducing bugs, all these improve quality. I strongly believe that a good design is essential for rapid software development.A good design is essential to maintaining speed in software development. Refactoring helps you develop software more rapidly, because it stops the design of the system from decaying. It can even improve a design.
听起来好像不对,当我讨论重构的时候,人们能很容易的想到它能改善代码的质量、设计和可读性,减少错误等等质量的改善。好的设计是软件开发过程中保证开发速度的根本。重构能让你快速编程,因为它能阻止系统设计的搁置,并能改善设计。
什么时候需要重构(When Should You Refactor? )
You don't decide to refactor, you refactor because you want to do something else, and refactoring helps you do that other thing.
不要决定什么时候重构,你要重构是因为你想做其它事情,而重构能帮助你做其它事情。
三法则(The Rule of Three)
Tip:Three strikes and you refactor.
建议:出现三次时,你需要重构。
Refactor When You Add Function
增加新功能时重构
Refactor When You Need to Fix a Bug
修改错误时重构
Refactor As You Do a Code Review
代码审查时重构
Problems with Refactoring
重构时碰到的问题
Databases
数据库
Changing Interfaces
接口改变
Tip: Don't publish interfaces prematurely. Modify your code ownership policies to smooth refactoring.
在接口未成熟之前不要发布。修改代码所有权策略而进行平稳的重构。
Design Changes That Are Difficult to Refactor
设计改变造成的重构困难
When Shouldn't You Refactor?
什么时候不需要重构?
太复杂而不必重构,直接重写好了;快到最后期限,不必重构。
重构与设计
Duplicated Code the same expression in two methods of the same class. the same expression in two sibling subclasses. duplicated code in two unrelated classes. Long Method The longer a procedure is, the more difficult it is to understand. Development environments that allow you to see two methods at once help to eliminate this step, but the real key to making it easy to understand small methods...
2013-06-21 17:23:22
Duplicated Code
the same expression in two methods of the same class.
the same expression in two sibling subclasses.
duplicated code in two unrelated classes.
Long Method
The longer a procedure is, the more difficult it is to understand.
Development environments that allow you to see two methods at once help to eliminate this step, but the real key to making it easy to understand small methods is good naming. If you have a good name for a method you don't need to look at the body.
A method with lots of parameters and temporary variables, these elements get in the way of extracting methods.
Large Class
When a class is trying to do too much, it often shows up as too many instance variables. When a class has too many instance variables, duplicated code cannot be far behind.
Long Parameter List
Divergent Change(发散式变化)
A list of signals of refactoring. This chapter is kind of abstract. It would be better to go back to the chapter after reading the rest concrete examples.
2014-12-21 15:50:46
A list of signals of refactoring. This chapter is kind of abstract. It would be better to go back to the chapter after reading the rest concrete examples.
Good example - Exact method - Test - Rename - Test - Move method (if necessary) and leave the old method to delegate to the new method - Test - Change all the callsites - Test - Remove old method - Test - Replace Temp with Query (with the cost of performance?) Replace Type with State/Strategy - Self encapsulate field: all uses of Type go through getting and setting methods - Class Movie { getCh...
2014-12-15 11:33:03
Good example
- Exact method
- Test
- Rename
- Test
- Move method (if necessary) and leave the old method to delegate to the new method
- Test
- Change all the callsites
- Test
- Remove old method
- Test
- Replace Temp with Query (with the cost of performance?)
Replace Type with State/Strategy
- Self encapsulate field: all uses of Type go through getting and setting methods
-
Class Movie
{
getCharge() {
switch (getPriceCode())
{
Case 1:
Case 2:
...
}
}
}
=>
Create
New Class Price
and
Subclasses of Price: RegularPrice, ChildPrice, etc
Class Movie
{
setPriceCode(int arg) {
switch (arg) {
Case 1: _price = new RegularPrice();
Case 2: ...
}
}
getCharge() {
return _price.getCharge();
}
}
Private Price _price;
Tips:
Rename variables for clarification
Change the program in small step
Duplicated Code the same expression in two methods of the same class. the same expression in two sibling subclasses. duplicated code in two unrelated classes. Long Method The longer a procedure is, the more difficult it is to understand. Development environments that allow you to see two methods at once help to eliminate this step, but the real key to making it easy to understand small methods...
2013-06-21 17:23:22
Duplicated Code
the same expression in two methods of the same class.
the same expression in two sibling subclasses.
duplicated code in two unrelated classes.
Long Method
The longer a procedure is, the more difficult it is to understand.
Development environments that allow you to see two methods at once help to eliminate this step, but the real key to making it easy to understand small methods is good naming. If you have a good name for a method you don't need to look at the body.
A method with lots of parameters and temporary variables, these elements get in the way of extracting methods.
Large Class
When a class is trying to do too much, it often shows up as too many instance variables. When a class has too many instance variables, duplicated code cannot be far behind.
Long Parameter List
Divergent Change(发散式变化)
0 有用 夢の點滴 2016-04-21 19:12:23
深受legacy code之苦
0 有用 Lucia 2014-10-31 12:50:18
可惜不是js,以及kindle看code真是看不进去
0 有用 optman 2006-12-24 21:47:28
3年前看的是草稿,电子版.秉承Martin Fowler的风格,很多的代码,很实用,就是有点不厌其烦,这或许就是老外写书的共同特点吧.
0 有用 hipresario 2016-06-24 21:42:49
Only understand 40%.
0 有用 坨坨 2013-07-23 09:38:46
函数粒度小一些有利于重构。当你复制粘贴代码时,或OO代码中较多条件判断时,考虑重构。用手机看代码真是虐。
0 有用 ljw7630 2022-03-13 17:48:59
当大牛在手把手教你认真对待每一行代码的时候 你还能说什么呢
0 有用 超级江湖骗子 2021-10-07 20:26:56
1 Extract function和split phase 我太赞同了。 其实还是本质还是鼓励small function,更清晰。 2 simplifying conditional logic 最喜欢的三个tool 2.1 decompose conditional 还是使用extract function,让复杂条件更清晰明了。 2.2 consolidate conditional lo... 1 Extract function和split phase 我太赞同了。 其实还是本质还是鼓励small function,更清晰。 2 simplifying conditional logic 最喜欢的三个tool 2.1 decompose conditional 还是使用extract function,让复杂条件更清晰明了。 2.2 consolidate conditional logic 相同逻辑使用一个if 2.3 replace nested conditional with guard clause if else的层次不要太深,影响阅读,影响理解。用简洁的几个if搞定。 3 抽象 抽象降低理解的困难度。 最重要的一点,重构的前提是单元测试。 (展开)
0 有用 夏嘉莫察瓦绒 2020-12-29 12:04:12
For much of the history of software development, most people believed that we design first, and only when done with design should we code. Over time, the code will be modified, and the integrity of th... For much of the history of software development, most people believed that we design first, and only when done with design should we code. Over time, the code will be modified, and the integrity of the system—its structure according to that design—gradually fades. The code slowly sinks from engineering to hacking. (展开)
0 有用 乐听_西东 2020-12-29 06:16:44
内容是okay 的,但中文翻译还是不太行。
0 有用 F | Wagon 2019-01-20 20:48:51
补