際際滷

際際滷Share a Scribd company logo
Refactoring Workshop
Consider a Simple Switch public bool EvaluateTestRun (TestStatus status) { switch (status) { case TestStatus.Pass:   return true; case TestStatus.Fail:   return false; case TestStatus.Irrelevent:   return true; } return false; } Are all swtich statements bad? What makes this bad? What makes this acceptable?
Consider a More Complex Switch public double CalculateStateSalesTax(decimal value, string state) { double returnValue = 0; switch (state) { case "Alabama": {returnValue = 4; break;} case "Alaska": {returnValue = 0; break;} case "Wyoming": {returnValue = 4; break;} } return returnValue; } Whats wrong here? How can this be improved?
Switch Replaced with Polymorphism public interface IState { double SalesTax { get;} } public interface IState { double SalesTax { get;} } public double RefactoredCalculateStateSalesTax(decimal value, string state) { IState stateObject = CreateState(state); return stateObject.SalesTax; }
Is This a Problem? if(UserAllowed == true) {   User.Allowed = true; } else {   User.Allowed = false; }
What about this? public void SendReminder() { StringBuilder message = new StringBuilder (); if (Data.ReminderDate == DateTime.Today) { if (Data.ShipDate > Data.OrderDate.AddDays(5)) { message.Append("Sorry last order was late."); message.Append("Next order will be ontime"); WriteMessage (message.ToString ()); } else { message.Append("Time to renew your subscription"; WriteMessage(message.ToString()); } } }
Do You See the Difference? public void SendReminder() { StringBuilder message = new StringBuilder (); if (Data.ReminderDate == DateTime.Today) { if (Data.ShipDate > Data.OrderDate.AddDays(5)) { message.Append("Sorry last order was late."); message.Append("Next order will be ontime"); } else { message.Append("Time to renew your subscription"; WriteMessage(message.ToString()); } } }
Complex Conditional

More Related Content

Refactoring workshop

  • 2. Consider a Simple Switch public bool EvaluateTestRun (TestStatus status) { switch (status) { case TestStatus.Pass: return true; case TestStatus.Fail: return false; case TestStatus.Irrelevent: return true; } return false; } Are all swtich statements bad? What makes this bad? What makes this acceptable?
  • 3. Consider a More Complex Switch public double CalculateStateSalesTax(decimal value, string state) { double returnValue = 0; switch (state) { case "Alabama": {returnValue = 4; break;} case "Alaska": {returnValue = 0; break;} case "Wyoming": {returnValue = 4; break;} } return returnValue; } Whats wrong here? How can this be improved?
  • 4. Switch Replaced with Polymorphism public interface IState { double SalesTax { get;} } public interface IState { double SalesTax { get;} } public double RefactoredCalculateStateSalesTax(decimal value, string state) { IState stateObject = CreateState(state); return stateObject.SalesTax; }
  • 5. Is This a Problem? if(UserAllowed == true) { User.Allowed = true; } else { User.Allowed = false; }
  • 6. What about this? public void SendReminder() { StringBuilder message = new StringBuilder (); if (Data.ReminderDate == DateTime.Today) { if (Data.ShipDate > Data.OrderDate.AddDays(5)) { message.Append("Sorry last order was late."); message.Append("Next order will be ontime"); WriteMessage (message.ToString ()); } else { message.Append("Time to renew your subscription"; WriteMessage(message.ToString()); } } }
  • 7. Do You See the Difference? public void SendReminder() { StringBuilder message = new StringBuilder (); if (Data.ReminderDate == DateTime.Today) { if (Data.ShipDate > Data.OrderDate.AddDays(5)) { message.Append("Sorry last order was late."); message.Append("Next order will be ontime"); } else { message.Append("Time to renew your subscription"; WriteMessage(message.ToString()); } } }