文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>步步为营 .NET 代码重构学习 十一

步步为营 .NET 代码重构学习 十一

时间:2011-06-02  来源:网络金领

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

动机(Motivation)

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

示例

07         {
08             if (people[i].Equals("Don"))
09             {
10                 found = "Don";
11             }
12             if (people[i].Equals("John"))
13             {
14                 found = "John";
15             }
16         }
17     }
18     SomeDataCode(found);
19 }

 

 

改为

07         {
08             if (people[i].Equals("Don"))
09             {
10                 found = "Don";
11                 break;
12             }
13             if (people[i].Equals("John"))
14             {
15                 found = "John";
16                 break;
17             }
18         }
19     }
20     SomeDataCode(found);
21 }

 

 

示例

07         {
08             if (people[i].Equals("Don"))
09             {
10                 found = "Don";                      
11             }
12             if (people[i].Equals("John"))
13             {
14                 found = "John";                      
15             }
16         }
17     }
18     return found;
19 }

 

改为

07         {
08             if (people[i].Equals("Don"))
09             {
10                 return "Don";
11             }
12             if (people[i].Equals("John"))
13             {
14                 return "John";
15             }
16         }
17     }
18     return string.Empty;
19 }

 

 

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

动机(Motivation)

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

示例

07         {
08             if (people[i].Equals("Don"))
09             {
10                 SendMail();
11                 return "Don";
12             }
13             if (people[i].Equals("John"))
14             {
15                 SendMail();
16                 return "John";
17             }
18         }
19     }
20     return string.Empty;
21 }

 

 

改为

13         {
14             if (people[i].Equals("Don"))
15             {
16                 return "Don";
17             }
18             if (people[i].Equals("John"))
19             {
20                 return "John";
21             }
22         }
23     }
24     return string.Empty;
25 }
26  
27 public void SendMailToPeople(string people)
28 {
29     if (!string.IsNullOrEmpty(people))
30         SendMail();
31 }

 

 

 

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

动机(Motivation)

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

示例

07     {
08         _height = value;
09         return;
10     }
11     if (name.Equals("width"))
12     {
13         _width = value;
14         return;
15     }
16 }

 

 

改为

排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载