ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Simplifying Conditional Expression and Making
Method Calls Simpler
Chris Chen
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
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
honestbee
SIMPLIFYING CONDITIONAL EXPRESSION
CH10CH9
honestbee
Decompose Conditional
• You have a complicated conditional (if-then-else) statement.
• Extract methods from the condition, then part, and else parts.
Before
After
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
honestbee
Consolidate Conditional Expression - Ands
Before
After
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
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
honestbee
Remove Control Flag - return
Before
After
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
honestbee
Replace Nested Conditional with Guard Clauses - Reversing the Conditions
Conditional
Before
After
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.
honestbee
Replace Conditional with Polymorphism
Before
honestbee
Replace Conditional with Polymorphism
After
honestbee
Introduce Null Object
• You have repeated checks for a null value.
• Replace the null value with a null object
Before
honestbee
Introduce Null Object
Before
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.
honestbee
Introduce Null Object
Step 2 Create a Factory
Method and force replace
Null Object with null case
honestbee
Introduce Null Object
Step 3 move default case
into Null Object
honestbee
MAKING METHOD CALLS SIMPLER
CH10CH9
honestbee
Rename Method
Before
After
• The name of a method does not reveal its purpose.
• Change the name of the method.
honestbee
Add Parameter
• A method needs more information from its caller
honestbee
Remove Parameter
• A parameter is no longer used by the method body.
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
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.
honestbee
Parameterize Method
Before
After
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
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
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
honestbee
Replace Parameter with Method
Step1 Extract a
method
honestbee
Replace Parameter with Method
Step 2 Remove
Parameter and temp
honestbee
Replace Parameter with Method
Step 3 create basic
price method and refine
get price method
honestbee
Introduce Parameter Object
• You have a group of parameters that naturally go together.
• Replace them with an object
honestbee
Introduce Parameter Object
Before
honestbee
Introduce Parameter Object
Step1 Create
DateRange Object and
put start-date, end-date
honestbee
Introduce Parameter Object
Step2 replace original
param with DateRange
honestbee
Introduce Parameter Object
Step3 add one method
includes in DateRange
class
honestbee
Remove Setting Method
• You have a group of parameters that naturally go together.
• Replace them with an object
honestbee
Remove Setting Method
Before
After
honestbee
Remove Setting Method
Before
After
honestbee
Hide Method
• A method is not used by any other class.
• Make the method private.
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
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
honestbee
Replace Error Code with Exception
• A method returns a special code to indicate an error.
• Throw an exception instead.
Before
After
honestbee
Thanks

More Related Content

Refactoring 9~10