examples for refactor

 讲多无谓,食多会滞,去片

详细请参考http://www.refactoring.com/catalog/index.html

要点: 

重构之前,首先检查自己是否有一套可靠的测试机制,这些测试必须有自我检验能力(self-checking).

重构技术是以微小的步骤修改程序,如果你犯下错误,很容易便可发现它.

更改变量名称,使代码更加清晰易读.任何一个傻瓜都能写出计算机可以理解的代码,唯有写出人类容易理解的代码,才是优秀的程序员.


定义常量代替hardcode的值:
Create a constant, name it after the meaning, and replace the number with it.
double potentialEnergy(double mass, double height) {
return mass * height * 9.81;
}

=====>
double potentialEnergy(double mass, double height) {
return mass * GRAVITATIONAL_CONSTANT * height;
}
static final double GRAVITATIONAL_CONSTANT = 9.81;

 

把方法的一部分变成一个静态的方法

 

 

Turn the method into its own object so that all the local variables become fields on that object. You can then decompose the method into other methods on the same object.

 

class Order...
	double price() {
		double primaryBasePrice;
		double secondaryBasePrice;
		double tertiaryBasePrice;
		// long computation;
		...
	}

 

to be continued... 

 

Replace Method with Method ObjectYou have a long method that uses local variables in such a way that you cannot apply Extract Method

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值