Professional Documents
Culture Documents
Matthias Ringwald
Software Engineering Workshop,
December 5-6, 2005
What is Refactoring?
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 2
Talk Overview
What is refactoring?
Why and when should you refactor?
Java refactorings in Eclipse
Advanced refactorings
Other languages & tools
Hints/Own Opinion/Take Home Message
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 3
Why should you Refactor?
Because Refactoring...
Improves the design of software
- the design of a program will decay over time
as people change code to realize short-term goal
Makes software easier to understand
- by refactoring, software gets cleaned up
Helps you to find bugs
- clarifying the structure exposes bugs
Helps you to program faster
- as a result from the previous three points
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 4
When to Refactor?
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 5
Bad Smells!
Duplicate Code
The #1 bad smell !
Long Method
Large Class
…
Switch Statements ( … breed duplication … )
Comments ( … not bad by itself …. )
... many more ( 23 in total )
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 6
Java refactoring in Eclipse
DEMO
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 7
Refactoring Example
Collapse Hierarchy
Situation
A superclass and subclass are not very different
Plan
Merge them together
Motivation
[some explanation]
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 8
Refactoring Example: Collapse Hierachy (2)
Mechanics:
Choose which class is going to be removed:
superclass or subclasses
Use Pull Up Field and Pull Up Method or Push Down
Field and Push Down Method
Compile and test with each move
Adjust references to the class that will be removed to
use the merged class. This will affect variable
declaration, parameter types, and constructors
Remove empty class
Compile and test
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 9
Refactoring and Design Patterns
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 10
Move Embellishment to Decorator
Situation:
Code provides an embellishment to a class' core
responsibility.
Plan:
Move the embellishment code to a Decorator.
Motivation:
Adding features makes a class complicated over time.
Place each embellishment in its own class and let that
class wrap the type of the object it needs to embellish.
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 11
Move Embellishment to Decorator (2)
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 12
Hints
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 13
Other Languages & Tools
List: www.refactoring.com/tools.html
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 14
Own Opinion
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 15
Take Home Message
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 16
Literature
www.refactoring.com
Martin Fowler's Site dedicated to Refactoring, more books there
Must-read introduction.
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 17
Why people hesitate
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 18
Bad Smells in Code
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 19
Advanced Refactorings
Composing Methods
Moving Features Between Objects
Organizing Data
Simplifying Conditional Expressions
Making Method Calls Simpler
Dealing with Generalization
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 20
Composing Methods
Extract Method.
Inline Method.
Inline Temp.
Replace Temp with Query.
Introduce Explaining Variable.
Split Temporary Variable.
Remove Assignments to Parameters.
Replace Method with Method Object.
Substitute Algorithm.
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 21
Moving Features Between Objects
Move Method.
Move Field.
Extract Class.
Inline Class.
Hide Delegate.
Remove Middle Man.
Introduce Foreign Method.
Introduce Local Extension.
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 22
Organizing Data
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 23
Organizing Data (2)
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 24
Simplifying Conditional Expressions
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.
Introduce Assertion.
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 25
Making Method Calls Simpler
Rename Method.
Add Parameter.
Remove Parameter.
Separate Query from Modifier.
Parameterize Method.
Replace Parameter with Explicit Methods.
Preserve Whole Object.
Replace Parameter with Method.
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 26
Making Method Calls Simpler (2)
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 27
11. Dealing with Generalization
Pull Up Field.
Pull Up Method.
Pull Up Constructor Body.
Push Down Method.
Push Down Field.
Extract Subclass.
Extract Superclass.
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 28
11. Deal with Generalization (2)
Extract Interface.
Collapse Hierarchy.
Form Template Method.
Replace Inheritance with Delegation.
Replace Delegation with Inheritance.
December 5, 2005 Matthias Ringwald, Institut for Pervasive Computing, ETH Zurich, mringwal@inf.ethz.ch 29