Refactoring A
Refactoring A
Martin Fowler
[email protected]
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