com s 362: object-oriented analysis and design refactoring

20
Com S 362: Object-Oriented Analysis and Design Refactoring

Upload: anis-hopkins

Post on 17-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Com S 362: Object-Oriented Analysis and Design Refactoring

Com S 362: Object-Oriented Analysis and Design

Refactoring

Page 2: 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

Page 3: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 4: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 5: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 6: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 7: Com S 362: Object-Oriented Analysis and Design Refactoring

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?

Page 8: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 9: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 10: Com S 362: Object-Oriented Analysis and Design Refactoring

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;}

Page 11: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 12: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 13: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 14: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 15: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 16: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 17: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 18: Com S 362: Object-Oriented Analysis and Design Refactoring

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

Page 19: Com S 362: Object-Oriented Analysis and Design Refactoring

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?

Page 20: Com S 362: Object-Oriented Analysis and Design Refactoring

20 Com S 362: Object-Oriented Analysis and Design

20

Com S 362: Object-Oriented Analysis and Design