Refactoring - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Refactoring

Description:

Change the structure, not the behavior. Example: (x2-1) = (x 1)(x-1) Cleans up ' ... Frank Lloyd Wright. How do we refactor? Manual Refactoring. Code Smells ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 18
Provided by: jason272
Category:

less

Transcript and Presenter's Notes

Title: Refactoring


1
Refactoring
  • Presented by Jason Lee

2
Overview
  • Defining Refactoring
  • Justification
  • Methodologies and Best Practices
  • Examples
  • Future Work
  • Questions

3
What is Refactoring?
  • Code reorganization
  • Implies equivalence
  • Change the structure, not the behavior
  • Example (x2-1) (x1)(x-1)
  • Cleans up messy code
  • Does NOT fix bugs

4
Refactoring and XP
  • XP uses extreme levels of good programming
    practices
  • The core practices of XP
  • Pair programming
  • Refactoring
  • Testing
  • Continuous integration
  • Evolutionary design

5
Costs and Benefits
  • Costs
  • Resource overhead
  • Requires experience
  • Benefits
  • Improves understandability
  • Simplifies testing
  • Uncovers bugs
  • "The two most important tools an architect has
    are the eraser in the drawing room and the sledge
    hammer on the construction site."
  • Frank Lloyd Wright

6
How do we refactor?
  • Manual Refactoring
  • Code Smells
  • Automated/Assisted Refactoring
  • Refactoring by hand is time consuming and prone
    to error
  • Eclipse (www.eclipse.org)
  • .NET Refactoring (www.dotnetrefactoring.com)
  • In either case, test your changes

7
Code Smells
  • A hint that something is wrong
  • Detection comes with experience
  • Table of common smells
  • http//wiki.java.net/bin/view/People/SmellsToRefac
    torings

8
Code Smell Examples
  • Too much code
  • Methods that are too big
  • Duplicated code
  • Code that is not executed
  • Not enough code
  • Classes with little code
  • Empty catch clauses
  • Outside the code
  • Excessive logging
  • Excessive commenting

9
Best Practices
  • Refactor in small steps
  • Do not change interface and implementation
    simultaneously
  • Minimize sources of error
  • Test continuously
  • Refactor with a purpose

10
Refactoring Examples
  • Renaming methods and variables
  • Extracting methods
  • Replace conditionals with polymorphism
  • Hide implementation with interface

11
Example 1Expansion and Contraction
  • Gray bars denote duplicated code

12
Example 2Collapsing Hierarchy
  • Centralize related behavior into single class

13
Example 3Indirection
  • Removes direct dependency of client code from the
    feature

14
Future Work
  • Refactoring noses
  • Refactoring SE artifacts
  • Inclusion into classical models of SE
  • Scalable refactoring

15
Summary
  • Refactoring does not affect behavior
  • Gaining experience and confidence is necessary
  • Use tools to reduce errors
  • Each behavior should do all of one thing, do one
    thing well, and do only one thing

16
References
  • 1 Smith, Suzanne, and Stoecklin, Sara. What We
    Can Learn from Extreme Programming. Consortium
    for Computing in Small Colleges, 2001.
  • 2 Fowler, Martin. Refactoring. ICSE 02, May
    19-25, 2002.
  • 3 Fowler, Martin. Refactoring Home Page.
    www.refactoring.com
  • 4 Mens, Tom, et al. Refactoring Current
    Research and Future Trends. LDTA 03 Preliminary
    Version, 2003.
  • 5 Wikipedia Refactoring. http//en.wikipedia.or
    g/wiki/Refactoring
  • 6 Wiki Pages about Refactoring.
    http//c2.com/cgi/wiki?WikiPagesAboutRefactoring

17
Questions
?
Write a Comment
User Comments (0)
About PowerShow.com