Your SlideShare is downloading. ×
Refactoring workshop
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Refactoring workshop

363

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
363
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
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

×