• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Refactoring Workshop
 

Refactoring Workshop

on

  • 762 views

Step by Step discussion of some common refactoring tasks

Step by Step discussion of some common refactoring tasks

Statistics

Views

Total Views
762
Views on SlideShare
760
Embed Views
2

Actions

Likes
1
Downloads
5
Comments
0

2 Embeds 2

http://localhost:7249 1
http://www.slideshare.net 1

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Refactoring Workshop Refactoring Workshop Presentation Transcript

    • 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;
      • }
      • What’s 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