《Refactoring-Improving the Design of Existing Code》读书笔记

重构的目的是让可以工作的代码更好,使面向对象的代码更简单,更容易维护。书上还说,所有的程序员应当能使用重构技术改进他们的代码。

 

nuts-and-bolts n.具体细节

 

前言

 

 

 

Chapter 3. Bad Smells inCode

stink v.发出臭味

 

决定什么时候去重构,什么时候该停止重构是非常重要的。

vague adj.含糊的

 

Duplicated Code

 

Number one in the stink(n.臭味) parade isduplicated code.如果你发现不止一处出现相同的代码结构,你可以肯定,如果你能找到办法统一他们的话,情况会更好。

 

1.如果在一个类的不同函数中有相同代码,你要做的就是把相同的代码提取出来做成函数,然后再调用它。

2.如果是在两个同属子类中有相同的代码,你可以通过把它们提到父类去。

3.If the code is similar but not thesame, you need to use Extract Method to separate the similar bits(n.片) from the different bits. You may then findyou can use Form TemplateMethod.

4.If the methods do the same thing witha different algorithm, you can choose the clearer of the two algorithms and useSubstitute Algorithm.

5.If you have duplicated code in twounrelated classes, consider using Extract Class in one class andthen use the new component in the other. Another possibility is that the methodreally belongs only in one of the classes and should be invoked by the otherclass or that the method belongs in a third class that should be referred to byboth of the original classes. You have to decide where the method makes senseand ensure it is there and nowhere else.

 

思考:

在我们的代码中分析重复代码出现的原因及为什么我们不愿意改,每种情况举例。

 

 

Long Method

 

The object programs that live best andlongest are those with short methods. Programmers new toobjects often feel that no computation ever takes place(没有计算发生过,换句话说,就是说前面结论是没有经过计算得出的), that object programs are endless sequences of delegation(面向对象程序是无止境的委托过程). When you have lived with such a program for a few years, however, you learnjust how valuable all those little methods are. All of the payoffs ofindirection-explanation, sharing, and choosing-are supported by little methods(see Indirection and Refactoring on page 61).(如何翻译?)

 

Since the early days of programmingpeople have realized that the longer a procedure is, the more difficult it isto understand(注:如果函数太长,我也觉得确实不太好看明白). 早期语言在函数调用上会付出一定的费用,这个原因deterred(v.阻止)people from small methods. Modern OO languages have prettymuch(几乎) eliminated that(pron.那个) overhead for in-process calls(注:完全同意). 对于代码阅读者仍然存在一个费用,因为你必须切换上下文去看子函数究竟做了什么(注:现在我也不喜欢搞太多的小函数,感觉函数多不容易从总体上把握一个类的所有功能?). 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 goodnaming. 实际上,如果你有一个好的函数名,你完全可以不去关注函数的实现。(注:对,不能取一个很好的名字也是我不想搞小函数的主要原因之一,另外一个原因就是对于函数的实现我们并不是一次就能确定的,如果实现发生改变,我们需要改动的地方很多,麻烦得很。还有就是设计函数的返回值和参数也很难。通过自己的代码总结自己不想写小代码的原因?)

 

The net(adj.最后的) effect(n.意思,大意) is that you should be much more(adv.更加)aggressive(adj.积极的)about decomposing methods(注:net effect真难翻译,不过翻译得还算满意,只要一意会就好理解了). A heuristic(注:实在不好翻译,记住这个词比较有用) wefollow is that whenever we feel the need to comment something, we write amethod instead. Such a method contains the code that was commented but(并且) is named after(按...命名) the intention(n.意图) of the code rather than(而不是) how it does it. We may do this ona group of lines or on as little as a single line of code.We do this even if the method call is longer than the code it replaces,provided the method name explains the purpose of the code(注:我觉得这样的做法确实是有意义的,黄建在这方面比我做得好,比如判断某个键是否按下,我就不愿意再次封装,他就愿意,但说实话,封装后的确实更容易看明白). The key here is not method length but the semantic distance(n.差异) between what the method does and how it doesit.

 

Ninety-nine percent of the time, allyou have to do to shorten a method is Extract Method. Find parts of themethod that seem to go nicely together and make a new method.If you have amethod with lots of parameters and temporary variables, these elements get in the way of(妨碍) extracting methods. If youtry to use Extract Method, you endup passing so many of the parameters and temporaryvariables as parameters to the extracted method that the result is scarcely(adv.简直没有) more readable than the original(如果企图采用Extract Method方式,你的结果是,这么多的参数和临时变量作为参数被传递到被提取出来的函数以至于跟先前比起来简直没有更多的可读性。). You can often use Replace Temp with Query to eliminate the temps.Long lists of parameters can be slimmed down with(用...来减少) Introduce Parameter Object and Preserve Whole Object.

 

如果你已经照那样做了, 但是 you still have too many temps and parameters, it's time to get out(v.弄出来) the

heavy artillery(注:是时候拿出重炮了): Replace Methodwith Method Object.

 

How do you identify(v.确定,识别) the clumps of code(代码片) to extract? A good technique is to look for comments.They often signal(v.以信号告知) this kind of semantic distance. A block ofcode with a comment that tells you what it is doing can be replaced by a methodwhose name is based on the comment. Even a

single line is worth extracting if itneeds explanation.

Conditionals and loops also give signsfor extractions. Use Decompose Conditional to deal

with conditional expressions. Withloops, extract the loop and the code within the loop into its own

method.

 

Large Class

 

When a class is trying to do too much,it often shows up(露面) as too many instancevariables. When a

class has too many instance variables,duplicated code cannot be far behind(重复代码就不远了)(怎么得出来的这个结论?).

 

You can Extract Class tobundle(vt.捆扎) anumber of the variables. Choose variables to go together in the component thatmakes sense for each(选择将将互相之间有意义的变量放到一个组件).For example, "depositAmount" and "depositCurrency" arelikely to belong together in a component. More generally, common prefixes orsuffixes for some subset of the variables in a class suggest the opportunityfor a component. If the component makes sense as a subclass, you'll find Extract Subclass often is easier.

 

Sometimes a class does not use all ofits instance variables all of the time. If so, you may be able to Extract Class or ExtractSubclass many times.

 

As with a class with too many instancevariables, a class with too much code is prime breeding groundn.滋生地) for duplicated code, chaos(n.混乱), and death. The simplest solution (have wementioned that we like simple solutions?) is to eliminate redundancy in theclass itself. If you have five hundred-line methods with lots of code incommon, you may be able to turn them into five ten-line methods with anotherten two-line methods extracted from the original.

 

As with a class with a huge wad(n.大量) of variables, the usual solution for a class with too much code iseither to Extract Class or ExtractSubclass. A useful trick is to determine how clientsuse the class and to use Extract Interface for each of these uses.That may give you ideas on how you can further break up the class.

 

If your large class is a GUI class, youmay need to move data and behavior to a separate domain object. This mayrequire keeping some duplicate data in both places and keeping the data insync. DuplicateObserved Data suggests how to do this. In this case,especially if you are using older Abstract Windows Toolkit (AWT) components,you might follow this by removing the GUI class and replacing it with Swingcomponents.

 

 

Comments

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值