25
Refactoring 2

Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Embed Size (px)

Citation preview

Page 1: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Refactoring 2

Page 2: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Admin• Blackboard• Quiz

Page 3: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Acknowledgements• Material in this presentation was

drawn from

Martin Fowler, Refactoring: Improving the Design of Existing Code

Page 4: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Refactorings (Fowler)•Add Parameter•Change Bidirectional Association to Unidirectional•Change Reference to Value•Change Unidirectional Association to Bidirectional•Change Value to Reference•Collapse Hierarchy•Consolidate Conditional Expression•Consolidate Duplicate Conditional Fragments•Convert Procedural Design to Objects

•Decompose Conditional•Duplicate Observed Data•Encapsulate Collection•Encapsulate Downcast•Encapsulate Field•Extract Class•Extract Hierarchy•Extract Interface•Extract Method•Extract Subclass•Extract Superclass•Form Template Method•Hide Delegate•Hide Method•Inline Class•Inline Method

Page 5: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Refactorings (Fowler)•Inline Temp•Introduce Assertion•Introduce Explaining Variable•Introduce Foreign Method•Introduce Local Extension•Introduce Null Object•Introduce Parameter Object•Move Field•Move Method•Parameterize Method•Preserve Whole Object•Pull Up Constructor Body•Pull Up Field•Pull Up Method

•Push Down Field•Push Down Method•Remove Assignments to Parameters•Remove Control Flag•Remove Middle Man•Remove Parameter•Remove Setting Method•Rename Method•Replace Array with Object•Replace Conditional with Polymorphism•Replace Constructor with Factory Method•Replace Data Value with Object•Replace Delegation with Inheritance

Page 6: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Refactorings (Fowler)•Replace Error Code with Exception•Replace Exception with Test•Replace Inheritance with Delegation•Replace Magic Number with Symbolic Constant•Replace Method with Method Object•Replace Nested Conditional with Guard Clauses•Replace Parameter with Explicit Methods•Replace Parameter with Method•Replace Record with Data Class

•Replace Subclass with Fields•Replace Temp with Query•Replace Type Code with Class•Replace Type Code with State/Strategy•Replace Type Code with Subclasses•Self Encapsulate Field•Separate Domain from Presentation•Separate Query from Modifier•Split Temporary Variable•Substitute Algorithm•Tease Apart Inheritance

Page 7: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Refactorings (Testing)•Inline Resource•Setup External Resource•Make Resource Unique

•Reduce Data•Add Assertion Explanation•Introduce Equality Method

Page 8: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Refactorings (Astels)

•Make Methods Independent•Replace Assert

Page 9: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Consolidate Conditional Expression• Multiple conditionals can be extracted into

method• Don’t do if conditions are really independent• Example

BEFORE

double diasabilityAmount() {

if (_seniority < 2) return 0;

if (_monthsDisabled > 12) return 0;

if (_isPartTime) return 0;

if (_isVeteran) return 50;

// Calculate disability amount

AFTER

double diasabilityAmount() {

if (isNotEligibleForDisability) return 0;

if (_isVeteran) return 50;

// Calculate disability amount

Page 10: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Duplicate Observed Data• Problem: You have data stored in a

GUI component and domain methods need access

• Solution: Copy the data into a domain object (so the methods can access it) and use an Observer to keep the two locations synchronized

Page 11: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Extract Class• Remove a piece of a class and make it

a separate class• Done when class is too big to

understand easily or behavior is not narrowly defined enough

• Indicated by having subsets of data & methods that go together, are changed together, or are dependent on each other

• Ask “What if I removed this? What would become useless?”

Page 12: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Extract Interface• Define an interface to move away from

a concrete implementation• Allows easier use of differing

databases or MockObjects

Page 13: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Extract Method• Pull code out into a separate method

when the original method is long or complex

• Name the new method so as to make the original method clearer

• Each method should have just one task• http://www.refactoring.com/catalog/

extractMethod.html

Page 14: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Extract Subclass• Used when a class has some behavior

used for some instances and not for others

• Make a subclass that inherits the common functionality

Page 15: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Introduce Assertion• Make implicit assumptions in the code

explicit• http://www.refactoring.com/catalog/i

ntroduceAssertion.html

Page 16: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Introduce Explaining Variable• Break up complex expressions into

chunks with clarifying names• http://www.refactoring.com/catalog/i

ntroduceExplainingVariable.html

Page 17: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Introduce Parameter Object• Replace a group of parameters that go

together with an object• Makes long parameter lists shorter &

easier to understand• Can indicate functionality to move to

new class• http://www.refactoring.com/catalog/i

ntroduceParameterObject.html

Page 18: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Preserve Whole Object• Send the whole object rather than long

lists of values obtained from the object• May increase dependency • A method that uses multiple values

from another class should be examined to determine if the method should be moved instead

• http://www.refactoring.com/catalog/preserveWholeObject.html

Page 19: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Rename Method• Method names should clearly

communicate the one task that the method performs

• If you are having trouble naming the method, it might be doing too much. Try extracting other methods first

Page 20: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Replace Conditional with Polymorphism• Replace switch statements with

polymorphic subclasses (or push case behavior down to existing subclasses)

• http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html

Page 21: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Replace Magic Number with Symbolic Constant

• Replace hard-coded literal values with constants

• Avoids duplication and shotgun surgery

• http://www.refactoring.com/catalog/replaceMagicNumberWithSymbolicConstant.html

Page 22: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Replace Nested Conditional with Guard Clauses

• In some conditionals, both paths are normal behavior & the code just needs to pick one

• Other conditionals represent uncommon behavior

• Use if/else with the normal behavior paths & guard clauses with uncommon behavior

• http://www.refactoring.com/catalog/replaceNestedConditionalWithGuardClauses.html

Page 23: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Replace Parameter with Method• A routine invokes a method just to

pass the result of that method on to another method

• Let the 2nd method call the first method directly (if it can)

• http://www.refactoring.com/catalog/replaceParameterWithMethod.html

Page 24: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

DeMorgan’s Law• Used for simplifying boolean

expressions• !(a && b) => (!a) || (!b)• !(a || b) => (!a) && (!b)

Page 25: Refactoring - refactorings - Testing Education · Acknowledgements • Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of Existing Code

Further resources• http://www.refactoring.com• http://c2.com/cgi/wiki?CodeSmell