com s 362: object-oriented analysis and design refactoring
TRANSCRIPT
Com S 362: Object-Oriented Analysis and Design
Refactoring
2 Com S 362: Object-Oriented Analysis and Design
2
Com S 362: Object-Oriented Analysis and Design
Recap: Class Diagrams Class diagrams represent design structure Three parts: name, attribute, operations Visibility, attribute type, multiplicity Association, association multiplicity Generalization i.e. interface impl, subclassing Composition i.e. class A contains class B Applied information hiding, DSM, layering
3 Com S 362: Object-Oriented Analysis and Design
3
Com S 362: Object-Oriented Analysis and Design
Recap: Interaction Diagrams Represent functionality Two major parts:
Life line boxes (represent life time of an object) Messages between objects (instances)
Also looked at some special case notations Instances: parameterized instances, singleton instances,
array elements Messages: creation, destruction, conditional messages,
mutually exclusive conditional messages, asynchronous messages, polymorphic messages
Abstractions: looping, iteration over collection, nesting of frames, decomposition
4 Com S 362: Object-Oriented Analysis and Design
4
Com S 362: Object-Oriented Analysis and Design
Recap: Responsibility Assignment Information Expert Creator Controller Polymorphism Pure Fabrication Indirection Protected Variations
5 Com S 362: Object-Oriented Analysis and Design
5
Com S 362: Object-Oriented Analysis and Design
Eclipse Tutorial Open source IDE
Available from http://www.eclipse.org
Lot of interesting & advance features
Demo: refactoring features in eclipse Extract method Pull up method Push down method
6 Com S 362: Object-Oriented Analysis and Design
6
Com S 362: Object-Oriented Analysis and Design
Refactoring Reorganize instructions in a programs,
perhaps rewriting
Key: Preserve semantics of the program
Objective: Improve structure (aka design), improve readability
Doesn’t add/remove any functionality
7 Com S 362: Object-Oriented Analysis and Design
7
Com S 362: Object-Oriented Analysis and Design
Value Argument Refactoring consumes resources
Doesn’t add any new feature Impact on near term profit = 0
Why Improve Structure?
8 Com S 362: Object-Oriented Analysis and Design
8
Com S 362: Object-Oriented Analysis and Design
Value Argument
Readability More readable less chances of inadvertent errors Adding features in future will be less cumbersome Invest today, reap later
Time
Understand-ability
Cost/ featur
e
9 Com S 362: Object-Oriented Analysis and Design
9
Com S 362: Object-Oriented Analysis and Design
Is semantics preserved? Informal & imprecise verification: Write test suite
Rigorous test suite development before refactoring Test after you refactor
Question: Why do I call it imprecise verification?
Formal verification: Prove that certain transformation always preserves the semantics
10 Com S 362: Object-Oriented Analysis and Design
10
Com S 362: Object-Oriented Analysis and Design
Refactoring that you don't see Compiler optimizations
Refactoring for performance improvement
Example code:
radius = ..;while(index < Length){array[index] = 2 * pi * radius;}
11 Com S 362: Object-Oriented Analysis and Design
11
Com S 362: Object-Oriented Analysis and Design
Loop Invariant Optimization
radius = ..;perimeter = 2 * pi * radiuswhile(index < Length){array[index] = perimeter;}
Compiler optimizations go through precise verification
12 Com S 362: Object-Oriented Analysis and Design
12
Com S 362: Object-Oriented Analysis and Design
Common Refactoring Techniques Extract method Pull up method Push down method Consolidate conditional expressions Encapsulate Collection … Alphabetical list at:
http://www.refactoring.com/catalog/index.html
13 Com S 362: Object-Oriented Analysis and Design
13
Com S 362: Object-Oriented Analysis and Design
Extract Method: The Problem
Frequent Renter Point
Movie
Presentation
Statement Logic
14 Com S 362: Object-Oriented Analysis and Design
14
Com S 362: Object-Oriented Analysis and Design
Pull Up Method/Field/Constructor
Fowler: Refactoring, Page 322
Methods with identical results on subclasses
Move the method to superclass
15 Com S 362: Object-Oriented Analysis and Design
15
Com S 362: Object-Oriented Analysis and Design
Push Down Method Behavior on a superclass is relevant only for
some of its subclasses.
Move the behavior to the subclass
Fowler: Refactoring - Page 328
16 Com S 362: Object-Oriented Analysis and Design
16
Com S 362: Object-Oriented Analysis and Design
Consolidate Conditional Expressiondouble disabilityAmount() {
if (_seniority < 2) return 0;
if (_monthsDisabled > 12) return 0;
if (_isPartTime) return 0;
// compute the disability amount
double disabilityAmount() {
if (isNotEligableForDisability()) return 0;
// compute the disability amount
Fowler: Refactoring - Page 240
CheckingEligibility for
disability
17 Com S 362: Object-Oriented Analysis and Design
17
Com S 362: Object-Oriented Analysis and Design
Encapsulate Collection Method returns a collection
Breaks encapsulation Allows clients to manipulate collection without objects’
knowledge
Make a read-only view, provide methods to add remove elements
Fowler: Refactoring - Page 208
18 Com S 362: Object-Oriented Analysis and Design
18
Com S 362: Object-Oriented Analysis and Design
Summary Improve the structure of code
No value gain at the moment Easier to add features later Less chances of errors in maintenance tasks
Key is to preserve semantics Imprecisely ensure that by developing tests Also, by code inspection
19 Com S 362: Object-Oriented Analysis and Design
19
Com S 362: Object-Oriented Analysis and Design
Exercise Can some refactoring techniques change the
design structure matrix view of the system?
If yes, which refactoring techniques change the DSM view and how?
20 Com S 362: Object-Oriented Analysis and Design
20
Com S 362: Object-Oriented Analysis and Design