Code refactoring
From Wikipedia, the free encyclopedia
Code refactoring is any change to a computer program's code that improves its readability or simplifies its structure without changing its results.
Contents |
[edit] Source code
In software engineering, "refactoring" a source code module often means modifying without changing its external behavior, and is sometimes informally referred to as "cleaning it up". In extreme programming and other agile methodologies, refactoring is an integral part of the software development cycle: developers alternate between adding new tests and functionality and refactoring the code to improve its internal consistency and clarity. Automatic unit testing ensures that refactoring does not make the code stop working.
Refactoring neither fixes bugs nor adds new functionality. Rather it improves the understandability of the code or changes its internal structure and design, and removes dead code, to make it easier for human maintenance in the future. In particular, adding new behavior to a program might be difficult with the program's given structure, so a developer might refactor it first to make it easier, and then add the new behavior. Refactoring is also a tool for removing bad code smells that exist in code.
Code refactoring alone does not change or add functionality to a system, but it can simplify future additions, application of design patterns, and reuse through polymorphism, which otherwise could be impractical, expensive, or unmaintainable.
An example of a trivial refactoring is to change a variable name into something more meaningful, such as from a single letter 'i' to 'interestRate' (see: identifier naming convention). A more complex refactoring is to turn the code within an if block into a subroutine. An even more complex refactoring is to replace an if
conditional with polymorphism.
While "cleaning up" code has happened for decades, the key insight in refactoring is to intentionally "clean up" code separately from adding new functionality, using a known catalogue of common useful refactoring methods, and then separately testing the code (knowing that any behavioral changes indicate a bug). The new aspect is explicitly wanting to improve an existing design without altering its intent or behavior. Optimization has nothing to do with refactoring. Before optimizing, one should first fully refactor the code so that code becomes easy to understand and hence easy to optimize.
Refactoring faces challenges in becoming accepted.[1] First, more study is needed on the long term effects of refactoring. Second, the people who pay the bill need to be informed about the effects, so they can see its benefit. Also, refactoring the business layer stored in a database schema is difficult or impossible, because of schema transformation and data migration that must occur while system may be under heavy use. Finally, refactoring that affects an interface can cause difficulties unless the programmer has access to all users of the interface. For example, a programmer changing the name of a method in an interface must either edit all references to the old name throughout the entire project or maintain a stub with the old method name. That stub would then call the new name of the method.
[edit] Antecedents
The term is by analogy with the factorization of numbers and polynomials. For example, x2 − x − 2 can be factored as (x + 1) (x − 2), revealing an internal structure that was previously not visible (such as the two roots at −1 and +2). Similarly, in software refactoring, the change in visible structure can often reveal the "hidden" internal structure of the original code.
The above math example illustrates a problem of "refactoring". One form is not necessarily objectively or universally superior to the other. They each emphasize different aspects of the expression and thus their utility may vary depending on the situation. This issue occurs in the software field where two or more professionals may have differing opinions on the ideal form of a given algorithm.
Refactoring is done as a separate step, to simplify testing. At the end of the refactoring, any change in behavior is clearly a bug and can be fixed separately from the problem of debugging the new behavior.
Martin Fowler's book Refactoring: Improving the Design of Existing Code[1] is the classic reference. Although refactoring code has been done informally for years, William Opdyke's 1993 Ph.D. dissertation[2] is the first known paper to specifically examine refactoring,[3] although all the theory and machinery have long been available as Program transformation systems. All of these resources provide a catalog of common methods for refactoring; a refactoring method has a description of how to apply the method and indicators for when you should (or should not) apply the method.
[edit] List of refactorings techniques
Here is a very incomplete list of code refactorings. A longer list can be found in Fowler's Refactoring book and in Fowler's Refactoring Website[4].
- allow for more abstraction
- Encapsulate Field - force code to access the field with getter and setter methods
- Generalize Type - create more general types to allow for more code sharing
- Replace type-checking code with State/Strategy
- Replace conditional with polymorphism
- breaking code apart into more logical pieces
- Extract Method (to turn part of a larger method into a new method. By breaking down code in smaller pieces, it is more easily understandable. This is also applicable to functions)
- Extract Class - move part of the code from an existing class into a new class
- improving names and location of code
- Move Method or Move Field - move to a more appropriate Class or source file
- Rename Method or Rename Field - changing the name into a new one that better reveals its purpose
- Pull Up - in OOP, move to a superclass
- Push Down - in OOP, move to a subclass
- Shotgun surgery - in OOP, when a single change affect many classes. In this case we keep all affecting code in a single class so that we make changes only at one place.
[edit] Forth
The term "factoring" has been used in the Forth community since at least the early 1980s. Chapter Six of Leo Brodie's book Thinking Forth (1984) is dedicated to the subject.
In Forth, factoring has essentially the same meaning that the Extract Method refactoring does in extreme programming—to break down a function (a "word" in Forth) into smaller, more easily maintained functions.
[edit] Automated code refactoring
Many software editors and IDEs have automated refactoring support. Here is a list of a few of these editors, or so-called refactoring browsers.
- IntelliJ IDEA (for Java)
- Eclipse's Java development kit (JDK)
- NetBeans (for Java)
- CodeGear Delphi
- Bicycle Repair Man (for Python, works with emacs and vi)
- Visual Studio 2005 (for .NET)
- ReSharper (An addon for Visual Studio)
- Visual Assist (An addon for Visual Studio with refactoring support for VB, VB.NET. C# and C++)
[edit] Etymology
The first known use of the term "refactoring" in the published literature was in a September, 1990 article by William F. Opdyke and Ralph E. Johnson.[5] Opdyke's Ph.D. thesis[2], published in 1992, also used this term.[3] The term "refactoring" was almost certainly used before then.
As a neologism, it is clearly a reference to mathematical factoring.
[edit] See also
- Code review
- Design pattern (computer science)
- Obfuscated code
- Peer review
- Rewrite (programming)
- Separation of concerns
- Test-driven development
- Unit testing
- Software Development Rhythms
[edit] Further reading
- Fowler, Martin (1999). Refactoring. Improving the Design of Existing Code. Addison-Wesley. ISBN 0-201-48567-2.
- Kerievsky, Joshua (2004). Refactoring To Patterns. Addison-Wesley. ISBN 0-321-21335-1.
- Wake, William C. (2003). Refactoring Workbook. Addison-Wesley. ISBN 0-321-10929-5.
- Feathers, Michael C (2004). Working Effectively with Legacy Code. Prentice Hall. ISBN 0-13-117705-2.
- Arsenovski, Danijel (2008). Professional Refactoring in Visual Basic. Wrox. ISBN 0-47-017979-1.
[edit] References
- ^ a b Fowler, Martin (1999). Refactoring. Addison-Wesley. ISBN 0-201-48567-2.
- ^ a b Opdyke, William F. "Refactoring Object-Oriented Frameworks" (compressed Postscript). Ph.D. thesis. . University of Illinois at Urbana-Champaign Retrieved on 2008-02-12.
- ^ a b Martin Fowler, "MF Bliki: EtymologyOfRefactoring"
- ^ Refactoring techniques in Fowler's refactoring Website
- ^ Opdyke, William F.; Johnson, Ralph E. (September 1990). "Refactoring: An Aid in Designing Application Frameworks and Evolving Object-Oriented Systems". Proceedings of the Symposium on Object Oriented Programming Emphasizing Practical Applications (SOOPPA), ACM.
[edit] External links
- What Is Refactoring? (c2.com article)
- Martin Fowler's homepage about refactoring
- Refactoring: Improving the Design of Existing Code by Martin Fowler available at Google Books
- Aspect-Oriented Refactoring by Ramnivas Laddad
- A Survey of Software Refactoring by Tom Mens and Tom Tourwé
- Refactoring To Patterns Catalog
- Refactoring Java Code
- Refactoring - elixir of youth for legacy VB code