refactoring workshop

8
Refactoring Workshop

Upload: nick-harrison

Post on 16-Jul-2015

377 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Refactoring workshop

Refactoring Workshop

Page 2: 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

Page 3: Refactoring workshop

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

Page 4: Refactoring workshop

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

Page 5: Refactoring workshop

Is This a Problem?

( == )if UserAllowed true{ . = ;User Allowed true}

else{ . = ;User Allowed false}

5

Page 6: Refactoring workshop

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

Page 7: Refactoring workshop

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

Page 8: Refactoring workshop

Complex Conditional

8