《重构-改善既有代码的设计》读书笔记
《重构-改善既有代码的设计》读书笔记
作者:Grey
原文地址:
以查询取代临时变量#
临时变量的问题在于:它们是暂时的,而且只能在所属函数内使用。由于临时变量只能在所属函数内可见,所以它们会趋势你写出更长的函数,因为只有这样你猜可以访问到所需要的临时变量,如果把临时变量替换为一个查询,那么同一个类中的所有函数都可以获得这份信息。
示例
double getPrice() {
int basePrice = number * itemPrice;
double discountFactor;
if (basePrice > 1000) discountFactor = 0.95;
else discountFactor = 0.98;
return basePrice * discountFactor;
}
将临时变量声明为final
double getPrice() {
final int basePrice = number * itemPrice;
final double discountFactor;
if (basePrice > 1000) discountFactor = 0.95;
else discountFactor = 0.98;
return basePrice * discountFactor;
}
把赋值动作的右侧表达式提炼出来
double getPrice() {
final int basePrice = getBasePrice();
final double discountFactor = getDiscount();
return getBasePrice() * discountFactor;
}
int getBasePrice() {
return number * itemPrice;
}
double getDiscount() {
if (getBasePrice() > 1000) return 0.95;
else return 0.98;
}
最后
double getPrice() {
final int basePrice = getBasePrice();
return getBasePrice() * getDiscount();
}
int getBasePrice() {
return number * itemPrice;
}
double getDiscount() {
if (getBasePrice() > 1000) return 0.95;
else return 0.98;
}
引入解释性变量#
你有一个复杂的表达式,将该复杂的表达式(或其中一部分)的结果放进一个临时变量,以此变量名称来解释表达式的用途。
if ((platform.toUpperCase().indexOf("MAC") > -1) &&
(browser.toUpperCase().indexOf("IE") > -1) &&
wasInitialized() && resize > 0)
{
// do something
}
可以重构为:
final boolean isMacOS = platform.toUpperCase().indexOf("MAC") > -1;
final boolean isIEBrowser = platform.toUpperCase().indexOf("IE") > -1
final boolean wasResized = resize > 0;
if (isMacOS && isIEBrowser && wasInitialized() && wasResized)
{
// do something
}
分解临时变量#
如果你的程序有某个临时变量赋值超过一次,它既不是循环变量也不是结果收集变量,这就意味着这个临时变量在函数中承担了一个以上的责任,如果临时变量承担多个责任,就应该针对每次赋值,创造一个独立,对应的临时变量。
double temp = 2 * (height + width);
System.out.println(temp);
temp = height * width;
System.out.println(temp);
应该改写成:
final double perimeter = 2 * (height + width);
System.out.println(perimeter);
final double area = height * width;
System.out.println(area);
以函数对象取代函数#
将这个函数放进一个单独的对象中,如此一来局部变量就成了对象内的字段,然后你可以在同一个对象中将这个大型函数分解为多个小型函数
class Account {
int gamma(int inputVal, int quantity, int yearToDate) {
int importantValue1 = (inputVal * quantity) + delta();
int importantValue2 = (inputVal * yearToDate) + 100;
if ....
....
....
return importantValue3 - 2 * importantValue1;
}
}
第一步:把这个函数变为一个函数对象,并加入一个构造函数
class Gamma {
private final Account account;
private int inputVal;
private int quantity;
private int yearToDate;
private int importantValue1;
private int importantValue2;
pirvate int importantValue3;
Gamma(Account account, int inputVal, int quantity, int yearToDate) {
this.xx = xx
...
...
...
}
}
现在可以把原本的函数搬到compute()中了,
int compute() {
int importantValue1 = (inputVal * quantity) + account.delta();
int importantValue2 = (inputVal * yearToDate) + 100;
if ....
....
....
return importantValue3 - 2 * importantValue1;
}
然后,我们修改旧的函数:
int gamma(int inputVal, int quantity, int yearToDate) {
return new Gamma(this, inputVal, quantity, yearToDate).compute();
}
作者:GreyZeng
出处:https://www.cnblogs.com/greyzeng/p/6181418.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
你可以在这里自定义其他内容
本文来自博客园,作者:Grey Zeng,转载请注明原文链接:https://www.cnblogs.com/greyzeng/p/6181418.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
2015-12-14 Github教程(0)