|
|||||||
Why refactoring?
Refactoring is vital to the success of the entire XP methodology. In the quote above, the Tortoise asserts that you can design a system while building it at the same time. One common objection to the XP process is that it is unstructured:
Refactoring allows a rapid development process and simple code structure while maintaining clean, scalable, and modular code. The importance of refactoringWithout rapid, early coding, projects
can spend many months in the design phase only to be discovered to be
impractical, overbudget, or overschedule once coding starts. Without
refactoring, a project can devolve into unusable, unmaintainable spaghetti code.
Refactoring also slightly delays the coding of important infrastructure. As stated above, the program is designed as it is written, not before. Important design and infrastructure details are laid out as necessary, not at the beginning of a project before the programmer/designer does not have the necessary information to make an informed decision. What is involved?There is no "canonical form" for code after which point refactoring is completely unnecessary. Code is refactored until it satisfies the teams notions of good XP code. There is often no end to refactoring, it is a process rather than a goal; one can never say "Function X is done being refactored." Refactoring is an art and thus requires some experience, but since many members of a team have the opportunity to refactor a piece of code, their collective experience often produces far better infrastructure than a "master planned" infrastructure designed by a smaller design team would.Characteristics to strive for in refactoring include:
Also, refactoring has nothing to do with adding more features. It is used on already sucessfully executing code and most often does not affect the function of that code, but only its structure. Implementation issuesIf refactoring is so great, why doesn't everyone do it?Refactoring means having to go back over one's own code. Especially when deadlines loom, working code is often assumed to be good-enough code. Refactoring can become another post-process like internal documentation that everyone recommends but that few programmers actually have the time to perform. As Cooper puts it in Extreme Programmming vs. Interaction Design, "There's enormous cost in writing code, but the real cost in writing code is that code never dies. If you can think this stuff through before you start pouring the concrete of code, you get significantly better results." This can be addressed through several means. If code is kept simple, then going back over and having to understand everything again is significantly easier and the programmer is more likely to do it. Also, XP advocates a principle known as Collective Code Ownership where everyone is responsible for the quality of the code as a whole, and has the ability to change any part of it. Thus, other programmers may refactor code if it was not abstracted to their satisfaction before; that is, if the code is too ugly (or smelly, in XP terminology), it will be noticed and fixed by the next person to work with it even if the original writer neglected this. Peer pressure can ensure that people don't neglect refactoring in the first place. Refactoring, since it only changes code structure and does not introduce new functionality, is sometimes so mechanical that it can be performed by a software tool, see the "Refactoring Tools" section of the Refactoring Home Page for more information on this. References:Extreme Programming article at Object Orientation Tips:http://ootips.org/xp.html Extreme Programming FAQ: http://www.jera.com/techinfo/xpfaq.html Misconceptions in XP II: Achilles and the Tortoise Discuss Design: http://www.xprogramming.com/xpmag/AchillesTortoise.htm Refactoring Home Page: http://www.refactoring.com/ "Refactor Mercilessly" from The Extreme Programming home page: http://www.extremeprogramming.org/rules/refactor.html Several Wiki entries: Refactor Mercilessly http://c2.com/cgi/wiki?RefactorMercilessly Refactor Improving The Design Of Existing Code http://c2.com/cgi/wiki?RefactoringImprovingTheDesignOfExistingCode Re Factor http://c2.com/cgi/wiki?ReFactor Refactoring Equals Reparameterization http://c2.com/cgi/wiki?RefactoringEqualsReparametrization What Is Refactoring http://c2.com/cgi/wiki?WhatIsRefactoring Last modified 1/23/02 by Jacob Mundt |