refactoring 9~10

46
Simplifying Conditional Expression and Making Method Calls Simpler Chris Chen

Upload: chris-chen

Post on 16-Mar-2018

31 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Refactoring   9~10

Simplifying Conditional Expression and Making

Method Calls Simpler

Chris Chen

Page 2: Refactoring   9~10

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

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

honestbee

SIMPLIFYING CONDITIONAL EXPRESSION

CH10CH9

Page 5: Refactoring   9~10

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

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

honestbee

Consolidate Conditional Expression - Ands

Before

After

Page 8: Refactoring   9~10

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

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

honestbee

Remove Control Flag - return

Before

After

Page 11: Refactoring   9~10

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

honestbee

Replace Nested Conditional with Guard Clauses - Reversing the Conditions

Conditional

Before

After

Page 13: Refactoring   9~10

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

honestbee

Replace Conditional with Polymorphism

Before

Page 15: Refactoring   9~10

honestbee

Replace Conditional with Polymorphism

After

Page 16: Refactoring   9~10

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

honestbee

Introduce Null Object

Before

Page 18: Refactoring   9~10

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

honestbee

Introduce Null Object

Step 2 Create a Factory

Method and force replace

Null Object with null case

Page 20: Refactoring   9~10

honestbee

Introduce Null Object

Step 3 move default case

into Null Object

Page 21: Refactoring   9~10

honestbee

MAKING METHOD CALLS SIMPLER

CH10CH9

Page 22: Refactoring   9~10

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

honestbee

Add Parameter

• A method needs more information from its caller

Page 24: Refactoring   9~10

honestbee

Remove Parameter

• A parameter is no longer used by the method body.

Page 25: Refactoring   9~10

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

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

honestbee

Parameterize Method

Before

After

Page 28: Refactoring   9~10

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

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

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

honestbee

Replace Parameter with Method

Step1 Extract a

method

Page 32: Refactoring   9~10

honestbee

Replace Parameter with Method

Step 2 Remove

Parameter and temp

Page 33: Refactoring   9~10

honestbee

Replace Parameter with Method

Step 3 create basic

price method and refine

get price method

Page 34: Refactoring   9~10

honestbee

Introduce Parameter Object

• You have a group of parameters that naturally go together.

• Replace them with an object

Page 35: Refactoring   9~10

honestbee

Introduce Parameter Object

Before

Page 36: Refactoring   9~10

honestbee

Introduce Parameter Object

Step1 Create

DateRange Object and

put start-date, end-date

Page 37: Refactoring   9~10

honestbee

Introduce Parameter Object

Step2 replace original

param with DateRange

Page 38: Refactoring   9~10

honestbee

Introduce Parameter Object

Step3 add one method

includes in DateRange

class

Page 39: Refactoring   9~10

honestbee

Remove Setting Method

• You have a group of parameters that naturally go together.

• Replace them with an object

Page 40: Refactoring   9~10

honestbee

Remove Setting Method

Before

After

Page 41: Refactoring   9~10

honestbee

Remove Setting Method

Before

After

Page 42: Refactoring   9~10

honestbee

Hide Method

• A method is not used by any other class.

• Make the method private.

Page 43: Refactoring   9~10

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

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

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

honestbee

Thanks