Step by step. Net code refactoring learning notes Series
Step by step. Net code refactoring
Step by step. Net code refactoring Study Notes II. Extraction Method (Extract Method)
Step by step. Net code refactoring study note 3. inline Method)
Step by step. Net code refactoring Study Notes 4. Temporary variables (temporary variable)
Step by step. Net code refactoring study note 5. decomposition functions and replacement algorithms (replace method and substitute algorithm)
Step by step. Net code refactoring study note 6. Move method and move field)
Step by step. Net code refactoring study note 7
Step by step. Net code refactoring learning notes 8
Step by step. Net code refactoring study note 9
Step by step. Net code refactoring study note 10
1. Remove control flag (remove control flag)
Motivation)
Replace the control mark with a break or return statement.
Example
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); }
Change
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); }
Example
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; }
Change
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; }
Ii. Replace nested conditional with guard clauses (replace nested conditional statements with guard statements)
Motivation)
The guard clause (guard clses) represents all special cases.
Example
public double GetPayAmount() { double result; if (IsDead) result = DeadAmount(); else { if (IsSeparated) result = SeparatedAmount(); else { if (IsRetired) result = RetiredPayAmount(); else result = NormalPayAmount(); } } return result; }
Change
public double GetPayAmount() { if (IsDead) return DeadAmount(); if (IsSeparated) return SeparatedAmount(); if (IsRetired) return RetiredPayAmount(); return NormalPayAmount(); }
Iii. Introduce Null Object (introducing Null Object)
Motivation)
Replace null value (invalid value) with Null Object (Invalid object)
Example
if (customer == null) plan = BillingPlan.Basic(); else plan = customer.GetPlan();
Change
public double GetPayAmount() { if (customer.IsNull()) plan = BillingPlan.Basic(); else plan = customer.GetPlan(); }
Iv. Rename method (rename function)
Motivation)
Modifying the function name makes it easy for people to understand its role.
Example
public int Getinvcdtlmt() { return 10; }
Change
public int GetInvoiceAbleCreditLimit() { return 10; }
5. Separate query from modifier (separate the query function from the modification function)
Motivation)
Create two different functions, one for query and the other for modification.
Example
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; }
Change
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(); }
Vi. parameterize method (parameters included in the function)
Motivation)
Create a single function to express those different values with Parameters
Example
public double TenPercentRaise() { return salary * 1.1; } public double FivePercentRaise() { return salary * 1.05; }
Change
public double Raise(double factor) { return salary * factor; }
VII. Replace parameter with explicit methods (replace the parameter with a definite function)
Motivation)
Create an independent function for each possible value of this parameter.
Example
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; } }
Change
private int _height; private int _width; public int Height { set { _height = value; } } public int Width { set { _width = value; } }