Upcoming SlideShare
×

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.
Standard text messaging rates apply

# Refactoring workshop

363

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
363
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

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 acceptable?
• 3. Consider a More Complex Switch
• public double CalculateStateSalesTax(decimal value, string state)
• {
• double returnValue = 0;
• switch (state)
• {
• case &quot;Alabama&quot;:
• {returnValue = 4; break;}
• {returnValue = 0; break;}
• case &quot;Wyoming&quot;:
• {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; }
• public void SendReminder()
• {
• StringBuilder message = new StringBuilder ();
• if (Data.ReminderDate == DateTime.Today)
• {
• {
• message.Append(&quot;Sorry last order was late.&quot;);
• message.Append(&quot;Next order will be ontime&quot;);
• WriteMessage (message.ToString ());
• }
• else
• {
• message.Append(&quot;Time to renew your subscription&quot;;
• WriteMessage(message.ToString());
• }
• }
• }
• 7. Do You See the Difference?
• public void SendReminder()
• {
• StringBuilder message = new StringBuilder ();
• if (Data.ReminderDate == DateTime.Today)
• {