步步为营 .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";
&nb
补充:Web开发 , ASP.Net ,