步步为营 .NET 代码重构学习笔记系列
一、Remove Control Flag(移除控制标记)
动机(Motivation)
以break语句或return语句取代控制标记。
示例
public void CheckSecurity(string[] people)
{
string found = string.Empty;
for (int i = 0; i < people.Length; i++)
{
if (found.Equals(""))
{
if (people[i].Equals("Don"))
{
found = "Don";
}
if (people[i].Equals("John"))
{
found = "John";
}
}
}
SomeDataCode(found);
}
改为
public void CheckSecurity(string[] people)
{
string found = string.Empty;
for (int i = 0; i < people.Length; i++)
{
if (found.Equals(""))
{
if (people[i].Equals("Don"))
{
found = "Don";
break;
}
if (people[i].Equals("John"))
{
found = "John";
break;
}
}
}
SomeDataCode(found);
}
示例
public string FindPeople(string[] people)
{
string found = string.Empty;
for (int i = 0; i < people.Length; i++)
{
if (found.Equals(""))
{
if (people[i].Equals("Don"))
{
found = "Don";
}
if (people[i].Equals("John"))
{
found = "John";
}
}
}
return found;
}
改为
public string FindPeople(string[] people)
{
string found = string.Empty;
for (int i = 0; i < people.Length; i++)
{
if (found.Equals(""))
{
if (people[i].Equals("Don"))
{
return "Don";
}
if (people[i].Equals("John"))
{
return "John";
}
}
}
return string.Empty;
}
二、Replace Nested Conditional with Guard Clauses(以卫语句取代嵌套条件式)
动机(Motivation)
使用卫语句(guard clauses)表现所有特殊情况。
示例
public double GetPayAmount()
{
double result;
if (IsDead)
result = DeadAmount();
else
{
if (IsSeparated)
result = SeparatedAmount();
else
{
if (IsRetired)
result = RetiredPayAmount();
else
result = NormalPayAmount();
}
}
return result;
}
改为
public double GetPayAmount()
{
if (IsDead)
return DeadAmount();
if (IsSeparated)
return SeparatedAmount();
if (IsRetired)
return RetiredPayAmount();
return NormalPayAmount();
}
三、Introduce Null Object (引入Null对象)
动机(Motivation)
将null value(无效值)替换为null object(无效物)
示例
if (customer == null)
plan = BillingPlan.Basic();
else
plan = customer.GetPlan();
改为
public double GetPayAmount()
{
if (customer.IsNull())
plan = BillingPlan.Basic();
else
plan = customer.GetPlan();
}
四、Rename Method(重新命名函数)
动机(Motivation)
修改函数名称让它人容易理解它的作用
示例
public int Getinvcdtlmt()
{
return 10;
}
改为
public int GetInvoiceAbleCreditLimit()
{
return 10;
}
五、Separate Query from Modifier(将查询函数和修改函数分离)
动机(Motivation)
建立两个不同的函数,其中一个负责查询,另一个负责修改。
示例
public string FindPeople(string[] people)
{
string found = string.Empty;
for (int i = 0; i < people.Length; i++)
{
if (found.Equals(""))
{
if (people[i].Equals("Don"))
{
SendMail();
return "Don";
}
if (people[i].Equals("John"))
{
SendMail();
return "John";
}
}
}
return string.Empty;
}
改为
public string FindPeople(string[] people)
{
string found = FindPeopleOne(people);
SendMailToPeople(found);
}
public string FindPeopleOne(string[] people)
{
string found = string.Empty;
for (int i = 0; i < people.Length; i++)
{
if (found.Equals(""))
{
if (people[i].Equals("Don"))
{
return "Don";
}
if (people[i].Equals("John"))
{
return "John";
}
}
}
return string.Empty;
}
public void SendMailToPeople(string people)
{
if (!string.IsNullOrEmpty(people))
SendMail();
}
六、Parameterize Method(令函数携带参数)
动机(Motivation)
建立单一函数,以参数表达那些不同的值
示例
public double TenPercentRaise()
{
return salary * 1.1;
}
public double FivePercentRaise()
{
return salary * 1.05;
}
改为
public double Raise(double factor)
{
return salary * factor;
}
七、Replace Parameter with Explicit Methods(以明确函数取代参数)
动机(Motivation)
针对该参数的每一个可能值,建立一个函数。
示例
private int _height;
private int _width;
public void SetValue(string name, int value)
{
if (name.Equals("height"))
{
_height = value;
return;
}
if (name.Equals("width"))
{
_width = value;
return;
}
}
改为
private int _height;
private int _width;
public int Height
{
set { _height = value; }
}
public int Width
{
set { _width = value; }
}