Refactoring workshop

472 views
426 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
472
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Refactoring workshop

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

×