refactoring workshop
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
2
Consider a More Complex Switch
public double( , CalculateStateSalesTax decimal value
)string state
{ = 0;double returnValue
( )switch state
{ " ":case Alabama
{ = 4; ;}returnValue break
" ":case Alaska
{ = 0; ;}returnValue break
" ":case Wyoming
{ = 4; ;}returnValue break
} ;return returnValue
}
• ?What’s wrong here
• How can this be?improved
3
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
}4
Is This a Problem?
( == )if UserAllowed true{ . = ;User Allowed true}
else{ . = ;User Allowed false}
5
What about this? ()public void SendReminder
{ = ();StringBuilder message new StringBuilder ( . == . )if Data ReminderDate DateTime Today { ( . > . . (5))if Data ShipDate Data OrderDate AddDays { . (" .");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 } }}
6
Do You See the Difference?
()public void SendReminder{ = ();StringBuilder message new StringBuilder ( . == . )if Data ReminderDate DateTime Today { ( . > . . (5))if Data ShipDate Data OrderDate AddDays { . (" .");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 } }}
7
Complex Conditional
8