惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
博客园 - Franky
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
C
Check Point Blog
爱范儿
爱范儿
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LangChain Blog
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
Microsoft Security Blog
Microsoft Security Blog
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)

博客园 - 网络金领

C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll] DataTable的数据批量写入数据库 C# .NET锁屏程序(顺带屏蔽任务管理器) 步步为营 .NET 代码重构学习笔记 十 步步为营 .NET 代码重构学习笔记 九 步步为营 .NET 代码重构学习笔记 八 步步为营 .NET 代码重构学习笔记 七 步步为营 .NET 代码重构学习笔记 六、移动函数和移动值域(Move Method And Move Field) 步步为营 .NET 代码重构学习笔记 五、分解函数和替换算法(Replace Method And Substitute Algorithm) 步步为营 .NET 代码重构学习笔记 四、临时变量(Temporary Variable) 步步为营 .NET 代码重构学习笔记 二、提炼方法(Extract Method) 步步为营 .NET 代码重构学习笔记 三、内联方法(Inline Method) 步步为营 .NET 代码重构学习笔记 一、为何要代码重构 步步为营 .NET 设计模式学习笔记 二十四、Factory Method(工厂方法模式) 步步为营 .NET 设计模式学习笔记 二十三、Interpreter(解释器模式) 步步为营 .NET 设计模式学习笔记 二十二、Memento(备望录模式) 步步为营 .NET 设计模式学习笔记 二十一、Visitor(访问者模式) 步步为营 .NET 设计模式学习笔记 二十、Mediator(中介者模式) 步步为营 .NET 设计模式学习笔记 十九、Chain of Responsibility(职责链模式)
步步为营 .NET 代码重构学习 十一
网络金领 · 2011-06-02 · via 博客园 - 网络金领

一、Remove Control Flag(移除控制标记)

动机(Motivation)

以break语句或return语句取代控制标记。

示例

01 public void CheckSecurity(string[] people)
03     string found = string.Empty;
04     for (int i = 0; i < people.Length; i++)
06         if (found.Equals(""))
08             if (people[i].Equals("Don"))
10                 found = "Don";
12             if (people[i].Equals("John"))
14                 found = "John";
18     SomeDataCode(found);

改为

01 public void CheckSecurity(string[] people)
03     string found = string.Empty;
04     for (int i = 0; i < people.Length; i++)
06         if (found.Equals(""))
08             if (people[i].Equals("Don"))
10                 found = "Don";
11                 break;
13             if (people[i].Equals("John"))
15                 found = "John";
16                 break;
20     SomeDataCode(found);

示例

01 public string FindPeople(string[] people)
03     string found = string.Empty;
04     for (int i = 0; i < people.Length; i++)
06         if (found.Equals(""))
08             if (people[i].Equals("Don"))
10                 found = "Don";                      
12             if (people[i].Equals("John"))
14                 found = "John";                      

改为

01 public string FindPeople(string[] people)
03     string found = string.Empty;
04     for (int i = 0; i < people.Length; i++)
06         if (found.Equals(""))
08             if (people[i].Equals("Don"))
10                 return "Don";
12             if (people[i].Equals("John"))
14                 return "John";
18     return string.Empty;

二、Replace Nested Conditional with Guard Clauses(以卫语句取代嵌套条件式)

动机(Motivation)

使用卫语句(guard clauses)表现所有特殊情况。

示例

01 public double GetPayAmount()
05         result = DeadAmount();
08         if (IsSeparated)
09             result = SeparatedAmount();
12             if (IsRetired)
13                 result = RetiredPayAmount();
15                 result = NormalPayAmount();

改为

01 public double GetPayAmount()
04         return DeadAmount();
06         return SeparatedAmount();
08         return RetiredPayAmount();
09     return NormalPayAmount();

三、Introduce Null Object (引入Null对象)

动机(Motivation)

将null value(无效值)替换为null object(无效物)

示例

2     plan = BillingPlan.Basic();
4     plan = customer.GetPlan();

改为

1 public double GetPayAmount()
3     if (customer.IsNull())
4         plan = BillingPlan.Basic();
6         plan = customer.GetPlan();

四、Rename Method(重新命名函数)

动机(Motivation)

修改函数名称让它人容易理解它的作用

示例

1 public int Getinvcdtlmt()

改为

1 public int GetInvoiceAbleCreditLimit()

五、Separate Query from Modifier(将查询函数和修改函数分离)

动机(Motivation)

建立两个不同的函数,其中一个负责查询,另一个负责修改。

示例

01 public string FindPeople(string[] people)
03     string found = string.Empty;
04     for (int i = 0; i < people.Length; i++)
06         if (found.Equals(""))
08             if (people[i].Equals("Don"))
10                 SendMail();
11                 return "Don";
13             if (people[i].Equals("John"))
15                 SendMail();
16                 return "John";
20     return string.Empty;

改为

01 public string FindPeople(string[] people)
03     string found = FindPeopleOne(people);
04     SendMailToPeople(found);
07 public string FindPeopleOne(string[] people)
09     string found = string.Empty;
10     for (int i = 0; i < people.Length; i++)
12         if (found.Equals(""))
14             if (people[i].Equals("Don"))
16                 return "Don";
18             if (people[i].Equals("John"))
20                 return "John";
24     return string.Empty;
27 public void SendMailToPeople(string people)
29     if (!string.IsNullOrEmpty(people))

六、Parameterize Method(令函数携带参数)

动机(Motivation)

建立单一函数,以参数表达那些不同的值

示例

1 public double TenPercentRaise()
3     return salary * 1.1;
6 public double FivePercentRaise()
8     return salary * 1.05;

改为

1 public double Raise(double factor)
3     return salary * factor;

七、Replace Parameter with Explicit Methods(以明确函数取代参数)

动机(Motivation)

针对该参数的每一个可能值,建立一个独立函数。

示例

04 public void SetValue(string name, int value)
06     if (name.Equals("height"))
08         _height = value;
11     if (name.Equals("width"))
13         _width = value;

改为

06     set { _height = value; }
11     set { _width = value; }