Refactoring Workshop

  • 522 views
Uploaded on

Step by Step discussion of some common refactoring tasks

Step by Step discussion of some common refactoring tasks

More in: Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
522
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
5
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. 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;
    • }
    • What’s 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());
    • }
    • }
    • }
  • 8. Complex Conditional