重构笔记

重构笔记

>总结翻译By joeyqzhou
>摘自 重构 Martin Fowler

- The first Step in refactoring is to build a solid set of tests
- It is vital to make tests self-checking
- 更改到更好的变量名是很有意义的
- code that communicates its purpose is very important. Refactor when you read some code

principles in refactoring

definition

  • Refactoring: a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior
  • 注意目标:easier to understand and cheaper to modify,(与优化性能是不相同的)
  • when you use refactoring to develop software, its about “adding functions” and refactoring

why should refactor

  • Refactoring improves the design of software ( eliminate duplicate code,you ensure that the code says everything once and only once, which is essence of good design)
  • Refactoring makes software easier to understand
  • help you find bugs
  • helps you program faster

When should you refactor

  • when you add a function
  • when you need to fix a bug
  • when you do a code review

when shouldn’t refactor

  • when code does not work (too many errors…)
  • close to deadline

Refactoring and Design

  • 一种思路upfront design:即提前设计好思路,找到最好的solution
  • with refactoring, you still do upfront design, but now you don’t try to find thee solution. (先大概设计好,然后不断重构)

Refactoring and performace

  • 有时候重构和性能是相互矛盾。(Changes that improve performance usually make the program harder to work with)
  • 较好的思路是,先不考虑性能(不要太离谱,太差),写出well-factored的代码,然后找性能的瓶颈,有针对的区优化

Bad smells in code

先记录下我可能用到的

  • Duplicated code
    • have same expression in two methods of same class
    • if you have duplicated code in two unrelated classes, consider using Extract class.
  • Long method
    • Extract method.(Find parts of the long method that work together and make a new method)
    • whenever you want to comment, consider to make a new method
    • the key to making it easy to understand small methods is good naming
    • even a single line is worth extracting if it needs explanation
  • large class
  • Long parameter List
    • Introduce parameter object (把多个参数,放在一起成为一个object)
    • preserve whole object (直接传入一个类,而不是这个类的方法或字段)
  • Divergent Change
    • It occurs when one class is commonly changed in different ways for different reasons
    • ideally, there is a one-to-one link between common changes and classes
  • Data clumps
    • same three or four data items together in lots of places
    • introduce parameter object and extract a class
    • dont worry about the new object only use some of the fileds
  • switch statements
    • switch statements is essentially that of duplication
    • consider polymorphism
  • temporary field
  • Comments (use small methods and appropriate name)

composing Methods 组织函数

  • Extract method(函数过长时,一小段代码可以重复利用时 VS inline method(A method’s body is clear as its name)
  • replace temp with expression/method (当临时变量只用1次或少次的时候,可以避免用临时变量
  • 当有一个比较复杂的expression时候,引入explaining variable(相对于上一条的反操作)
  • 当一个临时变量,被赋值多次。可以用多个临时变量来代替。(这样var->val, 多个临时变量的名字比一个变量的名字更容易读懂)
  • remove assignments to parameters (避免对参数赋值)
  • Replace method with method object (用object下面的函数代替函数,避免过多的local variable.因为这些可以作为object的字段)

Organizing Data

  • 封装private filed,设置set,get方法
  • replace data value with object (当多个数据经常组合在一起,应该把它们重构为一个object)
  • replace array with object (当数组的每一维都有一个特殊意义时,用object来代替)
  • 用一个有意义的常量代替 magic number

simplifying conditional expression 简化if

  • decompose condition

    • 分解if (如果if的条件很复杂或者内容很复杂,用函数去代替:条件,then,else. * if(method1) then method2 else method3
  • 合并多个if(如果多个if条件一样,合并后可以考虑用上一条)
  • 如果if,else中有相同的句子,提出来
  • replace conditional with polymorphism
  • replace null value with introduce null object ???

Making method call simpler

  • rename method
  • 多个功能的函数拆分为多个函数
  • preserve whole object (若需要一个object的某些字段,可以将object作为参数传入,这样避免后续增减参数.也可以使语义更强)
  • replace parameter with method (如果一个参数,可以在函数内计算出来,可以在函数中调用计算这个参数的方法,而不是传入该参数)
  • introduce parameter object (当多个参数同时出现多次的时候,若语义强,可以把多个参数作为一个object)
  • remove setting method (if a filed should be set at creation time and never altered)
  • replace error code with exception( a method return special code be replaced by throwing an error)
  • replace exception with test

再说 refactoring

  • Refactoring can help you leverage your past investment, reduce duplication, and streamline a program
  • Refactoring can help to restructure a program to make it easier to add new
    feature
  • Refactoring help programmers understand their code
  • AS refactoring becomes part of a routine, it stops feeling like overhead
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值