zoukankan      html  css  js  c++  java
  • 步步为营 .NET 代码重构学习 十一

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

    动机(Motivation)

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

    示例

    01 public void CheckSecurity(string[] people)
    02 {
    03     string found = string.Empty;
    04     for (int i = 0; i < people.Length; i++)
    05     {
    06         if (found.Equals(""))
    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 }

    改为

    01 public void CheckSecurity(string[] people)
    02 {
    03     string found = string.Empty;
    04     for (int i = 0; i < people.Length; i++)
    05     {
    06         if (found.Equals(""))
    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 }

    示例

    01 public string FindPeople(string[] people)
    02 {
    03     string found = string.Empty;
    04     for (int i = 0; i < people.Length; i++)
    05     {
    06         if (found.Equals(""))
    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 }

    改为

    01 public string FindPeople(string[] people)
    02 {
    03     string found = string.Empty;
    04     for (int i = 0; i < people.Length; i++)
    05     {
    06         if (found.Equals(""))
    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)表现所有特殊情况。

    示例

    01 public double GetPayAmount()
    02 {
    03     double result;
    04     if (IsDead)
    05         result = DeadAmount();
    06     else
    07     {
    08         if (IsSeparated)
    09             result = SeparatedAmount();
    10         else
    11         {
    12             if (IsRetired)
    13                 result = RetiredPayAmount();
    14             else
    15                 result = NormalPayAmount();
    16         }
    17     }
    18     return result;
    19 }

    改为

    01 public double GetPayAmount()
    02 {
    03     if (IsDead)
    04         return DeadAmount();
    05     if (IsSeparated)
    06         return SeparatedAmount();
    07     if (IsRetired)
    08         return RetiredPayAmount();
    09     return NormalPayAmount();
    10 }

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

    动机(Motivation)

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

    示例

    1 if (customer == null)
    2     plan = BillingPlan.Basic();
    3 else
    4     plan = customer.GetPlan();

    改为

    1 public double GetPayAmount()
    2 {
    3     if (customer.IsNull())
    4         plan = BillingPlan.Basic();
    5     else
    6         plan = customer.GetPlan();
    7 }

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

    动机(Motivation)

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

    示例

    1 public int Getinvcdtlmt()
    2 {
    3     return 10;
    4 }

    改为

    1 public int GetInvoiceAbleCreditLimit()
    2 {
    3     return 10;
    4 }

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

    动机(Motivation)

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

    示例

    01 public string FindPeople(string[] people)
    02 {
    03     string found = string.Empty;
    04     for (int i = 0; i < people.Length; i++)
    05     {
    06         if (found.Equals(""))
    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 }

    改为

    01 public string FindPeople(string[] people)
    02 {
    03     string found = FindPeopleOne(people);
    04     SendMailToPeople(found);
    05 }
    06  
    07 public string FindPeopleOne(string[] people)
    08 {
    09     string found = string.Empty;
    10     for (int i = 0; i < people.Length; i++)
    11     {
    12         if (found.Equals(""))
    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)

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

    示例

    1 public double TenPercentRaise()
    2 {
    3     return salary * 1.1;
    4 }
    5  
    6 public double FivePercentRaise()
    7 {
    8     return salary * 1.05;
    9 }

    改为

    1 public double Raise(double factor)
    2 {
    3     return salary * factor;
    4 }

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

    动机(Motivation)

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

    示例

    01 private int _height;
    02 private int _width;
    03  
    04 public void SetValue(string name, int value)
    05 {
    06     if (name.Equals("height"))
    07     {
    08         _height = value;
    09         return;
    10     }
    11     if (name.Equals("width"))
    12     {
    13         _width = value;
    14         return;
    15     }
    16 }

    改为

    01 private int _height;
    02 private int _width;
    03  
    04 public int Height
    05 {
    06     set { _height = value; }
    07 }
    08  
    09 public int Width
    10 {
    11     set { _width = value; }
    12 }
    http://u.huoban001.com/space.php
  • 相关阅读:
    springmvc
    POJ 3683 Priest John's Busiest Day
    POJ 3678 Katu Puzzle
    HDU 1815 Building roads
    CDOJ UESTC 1220 The Battle of Guandu
    HDU 3715 Go Deeper
    HDU 3622 Bomb Game
    POJ 3207 Ikki's Story IV
    POJ 3648 Wedding
    HDU 1814 Peaceful Commission
  • 原文地址:https://www.cnblogs.com/zpq521/p/2067574.html
Copyright © 2011-2022 走看看