100% found this document useful (1 vote)
106 views2 pages

Refactoring A

This document discusses refactoring as a technique for iterative software development. It allows developers to safely change a program's design through semantics-preserving transformations called refactorings. Refactoring tools can automate these transformations, but they can also be done manually with testing. Refactoring helps developers understand existing code and iteratively improve designs without introducing bugs. It is a valuable technique for developing and maintaining software.

Uploaded by

api-3773276
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
106 views2 pages

Refactoring A

This document discusses refactoring as a technique for iterative software development. It allows developers to safely change a program's design through semantics-preserving transformations called refactorings. Refactoring tools can automate these transformations, but they can also be done manually with testing. Refactoring helps developers understand existing code and iteratively improve designs without introducing bugs. It is a valuable technique for developing and maintaining software.

Uploaded by

api-3773276
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

METHODS IN PRACTICE

Martin Fowler
[email protected]

Refactoring: Doing Design


After the Program Runs
      for any length of local variables, you may have to pass in parameters, return a

I time, you should have heard about the notion of it-


erative development. The idea of iteration is that you
cannot get a design right the first time, you need to refine it as
you build the software. By iterating over the design several
value, or even not be able to do the extraction. The tool does
this analysis, prompts for the new method name, asks you to
name any parameters, and creates the new method with a call
from the old one. The program works exactly the same as it did
times, you get a better design. Even throwing away code before but is now a little easier for a human to read. This is im-
helps; indeed it is the sign of a good project that it does regu- portant: Any damn fool can write code that a computer can un-
larly throw away code. To not do so indicates a lack of learn- derstand, the trick is to write code that humans can understand.
ing—and keeping bad ideas around. The tool builder has to prove that the refactoring is seman-
Iteration is a great principle to discuss, but it has some tics preserving, then figure out how to provably carry out the
problems in practice. If you make a change to existing code, transformation in the code. It’s hairy stuff and requires a lot of
does that not carry the risk of introducing bugs? While people knowledge of compiler technology, but it can be done. The
have come up with various techniques and methods for design benefit is immediate. Once the transformation is encoded, you
in advance, there is not much discussion of how to apply them can use it with complete confidence. A semantics-preserving
in an iterative process, or how to do the iteration in a con- transformation is never going to add a bug to your program.
trolled and efficient manner. Tools like this are not science fiction. Bill Opdyke, while
Refactoring is the first technique I’ve come across that is a student at the University of Illinois, proved several of these
explicitly about doing iterative development in a controlled refactorings. Since then, two other graduate students, John
manner. It starts with software that currently works but is not Brant and Don Roberts, have produced a refactoring browser
well suited to an enhancement you wish to make. Refactoring for Smalltalk that implements these refactorings, and a few
is the controlled process of altering the existing software so more. If you are a Smalltalker, you should download it from
it’s design is the way you want it now, rather than the way you www.cs.uiuc.edu/users/droberts/Refactory.html.
wanted it then. It does this by applying a series of particular The refactoring browser is an awesome tool. With it I
code transformations, each of which are called refactorings. can safely reorganize some pretty ugly code (and yes even
Each refactoring helps change the code in a way that both is Smalltalk can get ugly). But what if you are working outside
rapid and does not introduce bugs. (Yes, I know that means of Smalltalk? Is refactoring still applicable? The answer is yes,
the word refactoring means two different things—I guess over- although not with tool support.
loading is just ingrained in this industry!)
How does refactoring do this magic? Essentially, there are two Refactoring Without Tools Although manual refactoring is
routes. The first is manual; the second relies on tools. Although not as easy, it is still possible—and useful. It boils down to two
using tools is less common at the moment, I’ll start with that. principles: take small steps and test frequently.
Humans can use the same semantics-preserving transforma-
Refactoring With Tools The essence of the tools-based ap- tion tools use, but in method extraction you have to look at the
proach is the notion of the semantics-preserving transformation. local variables yourself. It takes a little longer, but it isn’t too dif-
This is a transformation that you can prove will not change the ficult, because you’re looking at only a small section of code.
execution of the program. An example of this is a refactoring You then move the code over, put in the call, and recompile.
called extract method. If you have a long section of procedural If you did it correctly, you won’t get any bugs. Of course,
code, you can make it easier to read by taking a suitable chunk that’s a big if, so this is where tests come in. With manual
of the procedure and turning it into a separate method. To do refactoring you need to have a battery of tests that exercise
this with a tool, you select the code you wish to extract, and the the code sufficiently to give you confidence that you won’t in-
tool analyzes this code, looking for temporary variables and pa- troduce new bugs. If you build self-testing code, then you will
rameters. Depending on what the selected code does with these already have those tests. If not, you have to build them your-
self, but of course tests are useful anyway, since they make it
Martin Fowler is an independent consultant based in Boston, MA. easier to add new function as well as refactor.

www.DistributedComputing.com DISTRIBUTED
Computing 55
56
MindQ Ad

Refactoring to Understand Code When you follow a Its great strength is that it works on existing software. I’m
rhythm of small change, test, small change, test, you can make rarely faced with a green field. Often I have to work with
some remarkably large changes to a design. I’ve gone into some some form of existing code base. Refactoring provides a way
pretty nasty lumps of code, and after a few hours found class to manipulate and improve the code base without getting
structures that radically improve the software design. I don’t trapped in bugs.
usually have the design in mind when I start. I just go into the When working with new code, it gives developers the abil-
code and refactor initially just to understand how the code ity to change designs and make the iterative development pro-
works. Gradually, as I simplify the code, I begin to see what a cess much more controlled. I’ve noticed that it makes a signifi-
better design might be and alter the code in that direction. cant improvement to development speed. When trying to add
Refactoring to understand code is an important part of the new function to software, several projects have seen how it is
activity. This is obviously true if you are working with some- quicker to first refactor the existing software to make the
one else’s code, but it is often true with your own code as well. change easier. Thus I don’t recommend setting aside time to
I’ve often gone back to my own programming and not fully refactoring. It should be something you do because you need
understood what I was doing from the code, or gained a better to add a feature or fix a bug. That also makes it easier to justify
understanding by comparing it to later work. Of course, you to skeptical managers.
can do a similar thing by commenting, but I’ve found it better The biggest problem with refactoring at the moment is
to try to refactor the code so its intention is clear from the finding out more about how to do it. I’m in the process of
code, and it does not need so many comments. Some refactor- writing about what I, and various others, have learned about
ers go so far to claim that most comments are thus rendered the process, but that is still a way from publication. You can
unnecessary. I don’t go that far, but I do prefer to reach clarity find some more information, including references and an in-
through good factoring if I can. troductory example of refactoring, at ourworld.
compuserve.com/homepages/Martin_Fowler/.
The Value of Refactoring Refactoring has become a central I hope I’ve stimulated you to find out more about refactor-
part of my development process, and of the process I teach ing. I believe it is going to be one of crucial new developments
through my seminars and consulting work. It’s a design tech- of the next few years, and tools that include refactorings will
nique that is a great complement to the up-front design tech- be very important to software developers. I’d be interested to
niques advocated by the UML and various methods. know what you think; drop me a note at [email protected]. s

56 DISTRIBUTED
Computing September 1998

You might also like