<重构>笔记

重构:
在不改变的前提下,提高代码的可理解性,降低其修改成本


The rule of three
添加功能时一并重构
修补错误时一并重构
复审代码时一并重构

composing method

a. Extract method. divide a method into two or more method.
提炼方法, 把能分开,重用的部分单独做一个方法。

b. Inline method. conclude two or method into one method.
和Extract method相反, 概括多个方法成一个, 简化代码.

c. Inline temp. remove temp variable as we can.


d. Replace temp with query. change temp variable 's operation into a method that can be recall some other place


e. Introduce explaining variable. change series of operation into some explaining variable, use clear name


f. Split temporary variable. won't reuse temp variable for many set value operation, it make other confused


g. Remove assignments to parameters.
create a temp variable instead of object, java only use pass by value (参数不要用来赋值, 使用final, 避免对参数赋值)


h. Replace method with method object.
create method object( TestObject _testObject), use 'Extract method', use '_testObject.testMethod()'(method object)


i. Substitute algorithm. improve algorithm




Moving features between objects


a. Move method

b. Move field

c. Extract class.

d. Inline class.

e. Hide delegate. (对method进行加一层封装,使Client不能直接访问method. 简单委托关系)

f. Remove middle man. 和Hide delegate相反, 合理的隐藏程序基于实际系统的变化

g.Introduce foreign method. create new method, set server class entity as argument
ex: Date date = new Date(pro.getYear(), pro.getMonth(), pro.getDay()+1)
----> Date date = nextDay(pro);
staitc Date nextDay(Date pro)
{
return new Date()
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值