refactoring 101 william c. wake [email protected] 2-2-2000

16
Refactoring 101 William C. Wake [email protected] 2-2-2000

Upload: lawrence-pitts

Post on 05-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Refactoring 101

William C. [email protected]

2-2-2000

Page 2: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Refactoring

Improving the design of

existing code, without changing

its observable behavior.

Page 3: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Agenda

“Code smells” Solid set of unit tests Refactorings Demo Questions/Discussion

Page 4: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Code Smells“If it stinks, change it.”

Duplicate code Switch

statements Long method Large class Data class

(“data bag”)

Long parameter list

Primitive obsession

Temporary field etc.

Page 5: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Unit Tests

Automatic Self-checking Run often (almost constantly) “If you want to refactor, the

essential precondition is having solid tests.” -- M. Fowler

Page 6: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

JUnit

Unit testing tool Supports creating and running test

suites Available for Java & several other

languages http://xprogramming.com

Page 7: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Refactorings

Fowler’s book has a catalog There are many more Often reversible Pay attention to the mechanics Let the compiler tell you Small steps with constant tests

Page 8: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Extract Method

BEFOREvoid f() { …. // Compute score score = a * b + c; score -= discount;}

AFTERvoid f() { …. computeScore();}

void computeScore() { score = a * b + c; score -= discount;}

Page 9: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Encapsulate Field

BEFORE

public String name;

AFTERprivate String name;

public String getName() {return name;}

public String setName(String newName) { name = newName;}

Page 10: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Introduce Null Object

BEFORE

if (user == null) plan = Plan.basic();else plan = user.getPlan();

AFTER

Page 11: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Parameterize Method

BEFORE AFTER

WebService WebService

handleGet(…)

handlePut(…)

handle(serviceType,…)

Page 12: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Replace Constructor with Factory Method

BEFOREpublic User(int type) { this.type = type;}

AFTERUser(int type) { this.type = type;}

public static UsercreateUser(int type) { return new User(type);}

Page 13: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Replace String with Stringbuffer

BEFORE

String a, b, c; …return a + b + c;

AFTER

String a, b, c; …Stringbuffer result = new StringBuffer(a);result.append(b);result.append(c);return new String(result);

(Not in Fowler’s catalog)

Page 14: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Demo

Hand out real code seeded with refactoring opportunities

Audience takes 5 minutes to identify smells

Audience helps refactor in IDE Observe the motion back and forth

between test and code

Page 15: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

Discussion

Questions?

Page 16: Refactoring 101 William C. Wake William.Wake@acm.org 2-2-2000

More Information

Refactoring, by Martin Fowler Extreme Programming Explained,

by Kent Beck Anything by Jon Bentley -

Programming Pearls, More Programming Pearls, Writing Efficient Programs