refactoring 9~10
TRANSCRIPT
![Page 1: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/1.jpg)
Simplifying Conditional Expression and Making
Method Calls Simpler
Chris Chen
![Page 2: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/2.jpg)
honestbee
Outline - Simplifying Conditional Expression
• Decompose Conditional
• Consolidate Conditional Expression
• Consolidate Duplicate Conditional Fragments
• Remove control flag
• Replace Nested Conditional with Guard Clauses
• Replace Conditional with Polymorphism
• Introduce Null Object
![Page 3: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/3.jpg)
honestbee
Outline - Making Method Calls Simpler
• Rename Method
• Add Parameter
• Remove Parameter
• Separate Query from Modifier
• Parameterize Method
• Replace Parameterize with Explicit
Methods
• Preserve Whole Object
• Replace Parameter with Method
• Introduce Parameter Object
• Remove Setting Method
• Hide Method
• Replace Constructor with Factory
Method
• Encapsulate Downcase
• Replace Error Code with Exception
![Page 4: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/4.jpg)
honestbee
SIMPLIFYING CONDITIONAL EXPRESSION
CH10CH9
![Page 5: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/5.jpg)
honestbee
Decompose Conditional
• You have a complicated conditional (if-then-else) statement.
• Extract methods from the condition, then part, and else parts.
Before
After
![Page 6: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/6.jpg)
honestbee
Consolidate Conditional Expression - Ors
• You have a sequence of conditional tests with the same result.
• Combine them into a single conditional expression and extract it.
Before
After
![Page 7: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/7.jpg)
honestbee
Consolidate Conditional Expression - Ands
Before
After
![Page 8: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/8.jpg)
honestbee
Consolidate Duplicate Conditional Fragments
• The same fragment of code is in all branches of a conditional expression.
• Move it outside of the expression.
Before
After
![Page 9: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/9.jpg)
honestbee
Remove Control Flag - break
• You have a variable that is acting as a control flag for a series of boolean expressions.
• Use a break or return instead
Before
After
![Page 10: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/10.jpg)
honestbee
Remove Control Flag - return
Before
After
![Page 11: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/11.jpg)
honestbee
Replace Nested Conditional with Guard Clauses
• A method has conditional behavior that does not make clear the normal path of
execution.
• Use guard clauses for all the special cases.
Before
After
![Page 12: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/12.jpg)
honestbee
Replace Nested Conditional with Guard Clauses - Reversing the Conditions
Conditional
Before
After
![Page 13: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/13.jpg)
honestbee
Replace Conditional with Polymorphism
• You have a conditional that chooses different behavior depending on the type of
an object.
• Move each leg of the conditional to an overriding method in a subclass. Make the
original method abstract.
![Page 14: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/14.jpg)
honestbee
Replace Conditional with Polymorphism
Before
![Page 15: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/15.jpg)
honestbee
Replace Conditional with Polymorphism
After
![Page 16: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/16.jpg)
honestbee
Introduce Null Object
• You have repeated checks for a null value.
• Replace the null value with a null object
Before
![Page 17: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/17.jpg)
honestbee
Introduce Null Object
Before
![Page 18: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/18.jpg)
honestbee
Introduce Null Object
Step1 Create Null
Customer Class
If you aren't able to modify the Customer
class you can use a testing interface.
![Page 19: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/19.jpg)
honestbee
Introduce Null Object
Step 2 Create a Factory
Method and force replace
Null Object with null case
![Page 20: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/20.jpg)
honestbee
Introduce Null Object
Step 3 move default case
into Null Object
![Page 21: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/21.jpg)
honestbee
MAKING METHOD CALLS SIMPLER
CH10CH9
![Page 22: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/22.jpg)
honestbee
Rename Method
Before
After
• The name of a method does not reveal its purpose.
• Change the name of the method.
![Page 23: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/23.jpg)
honestbee
Add Parameter
• A method needs more information from its caller
![Page 24: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/24.jpg)
honestbee
Remove Parameter
• A parameter is no longer used by the method body.
![Page 25: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/25.jpg)
honestbee
Separate Query from Modifier
• You have a method that returns a value but also changes the state of an object.
• Create two methods, one for the query and one for the modification.
There is a simple example in the book P226
![Page 26: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/26.jpg)
honestbee
Parameterize Method
• Several methods do similar things but with different values contained in the method
body.
• Create one method that uses a parameter for the different values.
![Page 27: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/27.jpg)
honestbee
Parameterize Method
Before
After
![Page 28: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/28.jpg)
honestbee
Replace Parameterize with Explicit Methods
• You have a method that runs different code depending on the values of an enumerated parameter.
• Create a separate method for each value of the parameter
Before
After
![Page 29: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/29.jpg)
honestbee
Preserve Whole Object
• You are getting several values from an object and passing these values as parameters in a method call.
• Send the whole object instead.
Before
After
![Page 30: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/30.jpg)
honestbee
Replace Parameter with Method
• An object invokes a method, then passes the result as a parameter for a method.
• The receiver can also invoke this method. Remove the parameter and let the receiver invoke the method
Before
![Page 31: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/31.jpg)
honestbee
Replace Parameter with Method
Step1 Extract a
method
![Page 32: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/32.jpg)
honestbee
Replace Parameter with Method
Step 2 Remove
Parameter and temp
![Page 33: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/33.jpg)
honestbee
Replace Parameter with Method
Step 3 create basic
price method and refine
get price method
![Page 34: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/34.jpg)
honestbee
Introduce Parameter Object
• You have a group of parameters that naturally go together.
• Replace them with an object
![Page 35: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/35.jpg)
honestbee
Introduce Parameter Object
Before
![Page 36: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/36.jpg)
honestbee
Introduce Parameter Object
Step1 Create
DateRange Object and
put start-date, end-date
![Page 37: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/37.jpg)
honestbee
Introduce Parameter Object
Step2 replace original
param with DateRange
![Page 38: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/38.jpg)
honestbee
Introduce Parameter Object
Step3 add one method
includes in DateRange
class
![Page 39: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/39.jpg)
honestbee
Remove Setting Method
• You have a group of parameters that naturally go together.
• Replace them with an object
![Page 40: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/40.jpg)
honestbee
Remove Setting Method
Before
After
![Page 41: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/41.jpg)
honestbee
Remove Setting Method
Before
After
![Page 42: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/42.jpg)
honestbee
Hide Method
• A method is not used by any other class.
• Make the method private.
![Page 43: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/43.jpg)
honestbee
Replace Constructor with Factory Method
• You want to do more than simple construction when you create an object.
• Replace the constructor with a factory method
Before
After
![Page 44: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/44.jpg)
honestbee
Encapsulate Downcase
• A method returns an object that needs to be downcasted by its callers.
• Move the downcast to within the method.
• These case often appear with methods that return collection or iterator
Before
After
![Page 45: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/45.jpg)
honestbee
Replace Error Code with Exception
• A method returns a special code to indicate an error.
• Throw an exception instead.
Before
After
![Page 46: Refactoring 9~10](https://reader034.vdocuments.us/reader034/viewer/2022051504/5aabec787f8b9a893c8b46af/html5/thumbnails/46.jpg)
honestbee
Thanks