07-12-2012, 04:53 PM
Breaking the Barriers to Successful Refactoring: Observations and Tools for Extract Method
Breaking the Barriers to Successful Refactoring.PDF (Size: 286.09 KB / Downloads: 65)
ABSTRACT
Refactoring is the process of changing the structure of code
without changing its behavior. Refactoring can be semi-automated
with tools, which should make it easier for programmers to
refactor quickly and correctly. However, we have observed that
many tools do a poor job of communicating errors triggered by
the refactoring process and that programmers using them
sometimes refactor slowly, conservatively, and incorrectly. In this
paper we characterize problems with current refactoring tools,
demonstrate three new tools to assist in refactoring, and report on
a user study that compares these new tools against existing tools.
The results of the study show that speed, accuracy, and user
satisfaction can be significantly increased. From the new tools we
induce a set of usability recommendations that we hope will help
inspire a new generation of programmer-friendly refactoring tools.
INTRODUCTION
Refactoring is the process of changing the structure of a program
without changing the way it behaves. In his influential book on
refactoring, Fowler reports that Extract Method is one of the most
common refactorings that he performs [8, p.110]. Later, Fowler
says that Extract Method is “a key refactoring. If you can do
Extract Method, it probably means you can go on [to do] more
refactorings” [7]. However, as we will demonstrate, successfully
performing an Extract Method refactoring with a tool requires
more than the mere existence of the tool — it requires a tool that
is fast, error-resistant, and pleasant to use.
Refactoring and Refactoring Tools
Many activities fall under the heading of refactoring: changing
variable names, moving methods or fields up and down a class
hierarchy, and removing dead code, to name a few. Refactoring is
important to software development because it can aid in program
understanding and make it easier to add new features; thus,
refactoring can help programmers to adapt their software to
changing requirements.
However, performing a refactoring is not trivial, even for
seemingly simple refactorings such as changing an instance
variable name. First, you have to check that the new name is not
in use in the defining class, superclass, or subclasses. After
changing the variable name in its declaration, you must be sure to
change every old name to the new name, but not when the old
name appears in string literals, in the middle of other variable
names, or in comments (unless the comment directly refers to the
variable), and not when the old name refers to a local variable.
Some of this complexity arises from preconditions that must be
satisfied before we can be sure that a refactoring is safe. Opdyke
showed that program behavior is preserved when certain
preconditions are satisfied in the C++ programming
language [20]. At about the same time, Griswold defined
preconditions for meaning-preserving program transformations for
Scheme [9]. To automate the error-prone and time-consuming
task of checking preconditions by hand, Roberts and colleagues
developed a tool called the Refactoring Browser that
automatically checks preconditions before refactoring [23].
Although Roberts extolled the virtues of using refactoring tools,
he noted that the original Refactoring Browser was so unpopular
that even the tool’s designers did not use it [22]. After revising the
user interface of the tool, Roberts made three usability
recommendations: tools should be fast, have undo support, and be
tightly integrated into the programmers’ development
environment. Most tools appear to have implemented Roberts’
recommendations; among 16 refactoring tools, we found very
little variation from the revised Refactoring Browser’s user
interface.
The Extract Method Refactoring
One refactoring that has enjoyed widespread tool support is called
Extract Method. A tool that performs the Extract Method
refactoring takes a sequence of statements, copies them into a new
method, and then replaces the original statements with an
invocation of the new method. This refactoring is useful when
duplicated code should be factored out and when a long method
contains several code segments that are conceptually separate.
We will study the Extract Method tool in the Eclipse
programming environment [4]. We reason that the Extract Method
tool in Eclipse is worthy of study because it is a mature, nontrivial
refactoring tool and because most refactoring tool userinterfaces
are very similar.
To use the Eclipse Extract Method tool, the programmer first
selects code to be refactored, then chooses a refactoring to
perform, then configures the refactoring via a “refactoring
wizard,” and then presses “OK” to execute the refactoring. If there
is a precondition violation, the browser then presents the user with
a generic textual error message. Figure 1 displays an example of
such an error message in Eclipse. Figure 2 lists several
preconditions for the Extract Method refactoring.