Title: Matthew Wagner
1Refactoring Improving Your Code
- Matthew Wagner
- Cs564
- Fall 2004
2Refactoring
- What is Refactoring?
- Where did this idea come from?
- When do we Refactor?
- How do we Refactor?
- Are there tools to help us Refactor?
- Why should we Refactor?
- Why might we not want to Refactor?
- What data has been accumulated on Refactoring?
3Refactoring Book Definition
- Refactoring is the process of changing a
software system in such a way that it does not
alter the external behavior of the code yet
improves its internal structure. It is a
disciplined way to clean up code that minimizes
the chances of introducing bugs. In essence when
you refactor you are improving the design of the
code after it has been written. - Refactoring Improving the Design of Existing
CodeBy Martin Fowler, Kent Beck, John Brant,
William Opdyke, Don Roberts
4Refactoring In Laymans Terms
- Make small changes to code to improve code
structure, readability, and reusability.
5Refactoring Key Elements
- Simplifying complex statements
- Abstracting common solutions into reusable code
- Removal of duplicate code
6Short History
- Ward Cunningham and Kent Beck realize importance
of Refactoring while working with Smalltalk in
the 1980s. - William Opdykes 1992 doctoral thesis explores
the theoretical fundamentals for creating an
automated tool to perform safe refactorings. - In the late 90s John Brant and Don Roberts create
the Refactoring Browser'' for the Smalltalk
language.
7When to Refactor Smells in Code
- Refactor when the code smells.
- Smells are structures in the code that suggest
the possibility of refactoring. - http//wiki.java.net/bin/view/People/SmellsToRefac
torings
8Examples of Code Smells
- Comments
- Can quickly become verbose and reduce code
clarity. - Long Method
- The longer the method the harder it is to see
what its doing. - Uncommunicative Name
- Middle Man
- A class is delegating almost everything to
another class.
9Refactoring Methods
- Only refactor working code
- Refactorings should be Small
- Test often to avoid introducing bugs
- List of Refactorings
- http//www.refactoring.com/catalog
10Whats in a Refactoring?
- Name
- Summary
- Motivation
- Mechanics
- Examples
11Some Specific Refactorings
- Extract Method
- Use fragment to create method whose name explains
the purpose of the method. - Decompose Conditional
- Replace conditional logic with a method whose
name explains its purpose - Introduce Explaining Variable
- Put part of an expression in a temporary variable
with a name that explains the purpose. - Rename Member
- Change a name to be more descriptive.
12Extract Method
- Extract Method Use fragment to create method
whose name explains the purpose of the method.
Before Extract Method Refactoring
After Extract Method Refactoring
13Decompose Conditional
- Decompose Conditional Replace conditional
logic with a method whose name explains its
purpose
Before Decompose Conditional Refactoring
After Decompose Conditional Refactoring
14Introduce Explaining Variable
- Introduce Explaining Variable Put part of an
expression in a temporary variable with a name
that explains the purpose.
Before Introduce Explaining Variable Refactoring
Int sizeToAllocate charSize
ds.Tables0.Rows0FirstName.ToString().Lengt
h
After Introduce Explaining Variable Refactoring
Int numChars ds.Tables0.Rows0FirstName.T
oString().Length Int sizeToAllocate charSize
numChars
15Rename Member
- Rename Member Change a name to be more
descriptive.
Before Rename Member Refactoring
After Rename Member Refactoring
16Refactoring Tools
- Tool support makes refactoring faster and
therefore more productive. - Many feel that refactoring in not a feasible use
of time without proper tool support. - Most refactoring tools integrate into your
favorite IDE.
17Refactoring Tools - CRefactory
18Why Refactor?
- Refactoring Improves the Design of Software
- Refactoring Makes Software Easier to Understand
- Source code is the best documentation
- Refactoring Helps You Find Bugs
- Refactoring Makes Software Easier to Change
- Refactoring Helps You Program Faster
19Problems with Refactoring
- Changing working code may introduce bugs
- Changing Interfaces or Names can have widespread
effects - Some design decisions are difficult to refactor
later - Databases can be hard to refactor
- Less immediate benefit as the deadline approaches
- Endless Refactoring How much is enough?
20Effectiveness of Refactoring
- The Effects of Individual Practices on Software
Development Effort conducted at Pondicherry
University, India - Studied the effects of each of the 12 practices
of Extreme Programming, of which Refactoring is
one.
21Effectiveness of Refactoring
22Effectiveness of Refactoring
- The difference between no refactoring and
refactoring - as much as possible is only 30 developer-hours
- out of over 4000.
- This amounts to a 0.67 decrease in time spent,
- thanks to refactoring
- Is this enough to support Refactoring?
23Refactoring and Productivity
- Extreme Programming A Survey of Empirical Data
from a Controlled Case Study, conducted at VTT
Technical Centre of Finland
Productivity in Lines of Code per Hour and the
percent of Time spent refactoring during that
development period.
24References
- Refactoring Improving the Design of Existing
CodeBy Martin Fowler, Kent Beck, John Brant,
William Opdyke, Don Roberts - www.refactoring.com
- www.extremeprogramming.org
- www.xtreme-simplicity.net
- http//wiki.java.net/bin/view/People/SmellsToRefac
torings
25References (cont.)
- Abrahamsson, Pekka, et al. "Extreme Programming
A Survey of Empirical Data from a Controlled Case
Study." ACM-.IEEE International Symposium on
Empirical Software Engineering (ISESE 2004).
2004. - S. Kuppuswami,et al. The Effects of Individual
XP Practices on Software Development Effort.
Pondicherry University Computer Science
Department.