----
Chapter1:
1、fun太长,分割到其他类。
2、当需要给一段代码写注释时,往往可以把这段代码提取成一个fun。
3、少写重复代码。
4、多内聚,if can,把fun搬到该属于的类中。(大多数情况下,方法应该被移动到那些属性被使用的类中)
5、当需要给某段程序添加代码,感觉比较难添加时,先重构该程序使之易添加,再添加该代码。
6、重构前先建立测试环境,自检单元测试。
7、提取函数时,关注其内的变量,值不变的函数可以传进来,值变化的变量须格外关注。
8、重构须审慎小步进行,这样犯错的时候就容易发现。
9、好的代码需要清晰地传达她在做什么,而变量的命名是个关键因素。
10、傻瓜写的代码只有计算机看得懂,好的工程师写的代码人类看的懂。
----
1、优先使用对象组合而不是继承
2、一般建议构造函数中不包含太多逻辑
3、要投入时间和精力保持代码整洁、敞亮。
4、DRY原则: 系统中的每一项知识都必须具有单一、无歧义、权威的表示。
5、开发人员应该遵守人人为我,我为人人的黄金法则。
----
1、解决可读性差:能够清晰表达意图的好的命名;细粒度的方法;遵循单一职责原则的类;清晰的结构与模块的关系。
2、解决逻辑重复:遵循 DRY原则;合理利用封装;
3、解决添加困难:遵循开放封闭(OCP)原则:对扩展开放,对修改封闭。
4、解决复杂条件逻辑:封装或分解分支;以多态替代条件分支;好的方法命名。
----
1、接近项目晚期的时候不要重构,在那个点,往往利大于弊。
2、一旦你公开一个接口,你就不再可以安全地使用它了,还有编辑他的调用者。
3、Good programmers certainly have spent at least some timecleaning up their code.
----
1、重复代码->长方法->大类->过长参数
2、If you have a good look at me for a method you do not need tolook the body.
3、清晰代码的要诀就是保持方法的短小,以及能清晰表达意图的方法名。
4、方法名表达意图而不是做了什么
5、we fell the need to comment something, we write a methodinstead.Such a method contains the code that was commented but idnamed after the intention of the code rather than how it doesit.(根据代码的用途而非实现来命名,
6、好的方法名应该从调用者的角度来考虑,接口类似)
7、方法参数太多时,往往可以通过Replace Temp with Query来减少,实在不行再考虑Replace methodwith Method Object
8、单一职责原则要求,就一个类而言,应该只专注于做一件事和仅有一个引起变化的原因。
唯有专注,才能保证对象的高内聚;唯有单一,才能保证对象的细粒度。
When you feel the need to write a comment, first try to refactorthe code so that any comment becomes superfluous.
----
Chapter 6 Composing Methods
1、Extract Method 抽取方法 -> You have a code fragmentthat can be grouped togeter.(Turn the fragment into a method whosename explains the purpose of the method)
2、Inline Method 内联方法 -> A method's body is justas clear as its name.(Put the mothod's body into the body of itscallers and remove the mothod)
3、InLine Temp 内联临时变量。 ->You have a temp that isassigned to once with a simple expression, and the temp is gettingin the way of other refactorings.(Replace all references to thattemp with the expression.)
4、Replace Temp with Query。用查询代替局部变量->You areusing a temporary variable to hold the result of anexpression.(Extract the expression into a method.Replace allrefrences to the temp with the new method.The new method can thenbe used in other methods.)
(如果query method返回的结果每一次都不尽相同,则需要用临时变量保存querymethod的值,而不是任其被调用几次)
5、Introduce Explaining Variable 用解释型变量替换复杂表达式->You have a complicated expression,(Put the result of theexpression, or parts of the expression, in a temporary variablewith a name that explains the purpose)
6、Split Temporary Variable 分离变量-> You have atemporary Valiable assigned to more than once, but is not a loopvariable nor a collecting temporary variable.(Make a separatetemporary for each assignment)
7、Remove Assignments to Parameters 消除直接修改入参。 ->The code assigns to a parameter.(Use a temporary bariableinstead)
8、Replace Method with Method Object.用方法对象代替方法。 You have a longmethod that uses local variables in such a way that you cannotapply Extract Mehod.(Turn the method into its own object so thatall the local variables become fields on that object.You can thendecompose the method into other methods on the same object)
9、Substitute Algorithm.优化内部算法 -> You want toreplace algorithm with one that is clearer.(Replace the body of themethod with the new algorithm)
Chapter 7 Moving Fratures Between Objects
Often classes becoe bloated with too many responsibilities.Inthis case I use Extract Class to separate some of theseresponsiblities.
1、Move method. 移动类方法-> A method is, or will be,using or used by more features of another class than on which it isdefined.(Create a new method with a similar body in the class ituses most.Either turn the old method into a
需要将该方法移动到类B中。)
2、Move Field.移动类属性 -> A feild is, or will be,used by another class more than the class on which it isdefined.(Create a new field in the target class, and change all itsuser)
3、Extract Class. 提取类-> You have one class doingwork that should be done by two.(Create a new class and move therelevantfields and methods from the old class into the newclass)
4、Inline Class. 内联类(同上反) -> A class is not doingvery much.(Move all its features into another class and deleteit)
5、Hide Delegate. 隐藏委托 -> A client is calling adelegate class of an object.(Create methods on the server to hidethe delegate)
6、Remove Middle Man. 消除中间人(同上反) -> A class isdoing too much simple delegation.(Get the client to call thedelegate directly)
7、Introduce Foreign Method. 引入外函数 -> A serverclass you are using needs an additional method, but you can notmodify the class.(Create
1、Self Encapsulate Field 自封装属性 -> You areaccessing a field directly, but the coupling to the field isbecoming awkward.(Create getting and setting methods for the fieldand use only
2、Replace Data Value with Object. 用对象代替数据值 -> Youhave a data item that needs additional data or behavior.(Turn thedata item into an object)
3、Change Value toReference.修改值为引用[新建的可以是同一个对象](用一个容器HashTable)保存值,新建时有的直接里面取)-> You hava a class with many equal instances thatyou want to replace with a single object.(Turn the object into areference object)
(Reference objects are things like customer or account. Each objectstands for one object in the real world, and you use the objectidentity to test whether they are equal.
Value objects are things like date or money. They are definedentirely through their data values.系统中可能有多个这样的值,如“2013.06.10”,所以你需要写equals 方法和HashCode)
4、Change Reference to Value.修改引用为值[新建的是不同对象] ->You have a reference object that is small, immutable, and awkwardto manage. (Turn it into a value object)
5、Replace Array withObject.用对象代替数组(里面不是同类型的数据)->You hava an array inwhich certain elements mean different things.(Replace the arraywith an object that has a field for each element)
6、Duplicate Observed Data.重复的被观察数据 -> You havadomain data available only in a GUI control, and domain methodsneed access.(Copy the data to a domain object. Set up an observerto synchronize the two pieces of data)
7、Change Unidirectional Association to Bidirectional.改变单向关系为双向-> You have two classes that need to use eachother's features, but there is only a one-way link.
8、Change Bidirectional Association to Unidirectional.改变双向关系为单向-> You have a two-way association but one class nolonger needs features from the other.(Drop the unneeded end of theassociation)
9、Replace Magic Number with Symbolic Constant. 用常量代替魔鬼名字
10、Encapsulate Field. 封装属性。-> There is a publicfield.(Make it private and provide accessors)
1、Replace Record with Data Class. 用数据类替换记录 -> Youneed to interface with a record structure in a traditionalprogramming environment.(Make a dumb object for the record.)
2、Replace Type Code with Class.用类替换类型编码(这条没用, 枚举就可以搞定)-> A class has a numeric type code that does notaffect its behavior.(Replace the number awith a new class)
3、Replace Type Code with Subclasses. 用子类代替类型编码.-> You have an immutable type code affects thebehavior of a class.(Replace the type code with subclasses)
4、Replace Type Code with State / Strategy.用状态模式代替类型编码 You have atype code that affects the behavior of a class, but you cannot usesubclassing.
5、Replace Subclass with Fields.用属性代替子类. -> Youhave subclasses that vary only in methods that return constantsdata.(Change the methods to superclass fields and eliminate thesubclasses)
6、Replace Conditional with Polymorphism 用多态替换条件表达式-> You have a conditional that chooses differentbehavior depending on the type
7、Introduce null Object 引入空对象 -> You haverepeated checks for a null value.(Replace the null value with anull object)
8、Other Special Cases (如NAN)
9、Introduce Assertion. 引入断言 -> A section of codeassumes something about the state of the program(Make theassumption explicit an assertion)
----
Chapter 10
Coming up with interfaces that are easy to understand and use isa key skill in developing good object-oreiented software。
Good interfaces show only what they have to and no more.
1、Rename method 重命名 -> The name of a method doesnot reveal its purpose.(Change the name of the method)
2、Add Parameter 添加参数 -> A method needs moreinformation from its caller.(Add a parameter for an object that canpass on this information)
3、Remove Parameter. 删除参数 -> A parameter is nolonger used by the method body.(Remove it)
4、Separate Query from Modifier 从修改中分离查询 -> Youhave a method that returns a value but also changes the state of anobject.(Create two
5、Parameterize method. 参数化方法[类似的代码段或方法用带参数的方法替换]-> Several methods do similar things but withdifferent values contained in the method body.(Create onemethod
6、Replace Parameter with Explicit Methods 用清晰的方法代替参数->
2013.7.24
7、Preserve Whole Object
8、Replace parameter with Method 用方法代替参数 -> Anobject invokes a method, then passes the result as a parameter fora method. The receiver can also invoke this method(Remove
9、Introduce Parameter Object 引入参数对象 -> Youhave
10、Remove Setting Method 移除设置函数 -> A field should beset at creation time and never altered.(Remove any setting methodfor that field)
2013.07.25
11、Hide Method 隐藏方法 -> A method is not used byany other class.(Make the method private)
12、Replace Constructor with Factory Method
13、Encapsulate Downcast -> 封装往下类型转换 A methodreturns an object that needs to be downcasted by its callers.(Movehe downcast to within the method)
2013.07.27
14、Replace Error Code with Exception 用异常代替错误码 ->A method returns a special code to indicate an error.(Throw anexception instead)
15、Replace Exception with Test 用条件判断检查代替异常 -> Youare throwing an exception on a condition the caller could havechecked first.(Change the caller to make the test first)
2013.07.28
Chapter 11
1、Pull Up Field 提升属性 -> Two subclass have thesame field.(Move the field to the superclass)
2、Pull Up Method 提升方法 -> You have methods withidentical results on subclasses.(Move them to the superclass)
3、Pull Up Constructor Body 提升构造函数体 -> You haveconstructors on subclasses with mostly identical bodies.(Create asuperclass constructors; call this from the subclass methods)
4、Pull Down Method 下放函数 -> Behavior on asuperclass is relevant only for some of its subclasses. Move it tothose subclasses.
5、A field is used only by some subclasses. Move the field tothose subclasses.
6、Extract Subclass 提取子类 -> A class has featuresthat are used only in some instances.Create a subclasses for thatsubset of features.
7、Extract Superclass 提取父类 -> You have two classeswith similar features. Create a superclass and move the commonfeatures to the superclass.
8、Extract Interface 提取接口 -> Several clients usethe same subset of a class's interface, or two classes have part oftheir interfaces in common.
2013.08.10
9、Collapse Hierachy 合并继承层次 -> A superclass andsubclass are not very different.(Merge them together)
10、From Template Method 塑造模板函数 -> You have twomethods in subclasses that perform similar steps int the sameorder, yet the steps are diffrent.(Get the steps into methods withthe same signature, so taht original methods become the same. Thenyou can pull them up)
11、Replace Inheritance with Delegation 用委托代替继承 -> Asubclass uses only part of a superclasses inerface or does not wantto inherit data.(Create a feild for the superclass, ajust methodsto deltegate to the superclass, and remove the subclassing)
12、Replace Delegation with Inheritance 用继承代替委托 ->You are using delegation and are often writing many simpledelegations for the entire inerface.(Make the delegating class asublass of the delegate)
Chapter 12 Big Refactorings
1、Tease Apart Inheritance
2、Covert Procedural Design to Objects 将过程式的设计转化为对象-> You have code written in a procedural style.(Turnthe data records into objects, break up the behavior, and move thebehavior to the objects)
3、Separate Domain from Presentation 将领域逻辑与表现分离 ->You have GUI classes tht contain domain logic .(Separate the domainlogic into separate domain classes)
4、Extract Hierarchy 提取继承层次 -> You have a classthat is doing too much work, at least in part through manyconditional statements.(Create a hierarchy of classes in which eachsubclass represents a special case)
相关优秀书籍:
《More Effective C++35个改善编程与设计的有效方法》
《More Exceptional C++》 中文版 Herb Sutter
《提高C++编程性能的编程技术》Dov Bulka / David Mayhew
《More Effective C++改善程序与设计的55个具体做法》
《程序员修炼之道 - 从小工到专家》Andrew Hunt / David Thomas
《UNIX编程艺术》 Eric S.Raymond