Code refactoring
From Wikipedia, the free encyclopedia
- See also: Wikipedia:Refactoring talk pages
A code refactoring is any change to a computer program which improves its readability or simplifies its structure without changing its results.
When refactoring, best practice[citation needed] is to have test fixtures in place: They can validate that your refactorings don't change the behavior of your software[citation needed] (see test-driven development).
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 change its structure and design, and remove 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 easy, and then add the new behavior.
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.
[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 formula and thus their utility may vary depending on both the situation of usage and on the personality or style of the mathematician. 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 is the classic reference. Although refactoring code has been done informally for years, William Opdyke's 1993 PhD dissertation [1] is the first known paper to specifically examine refactoring, 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.
Refactoring is thought by many to be an important concept. It has been named as one of the most important software innovations by author and commentator David A. Wheeler.
[edit] List of refactorings
- Encapsulate Field
- 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)
- Generalize Type
- Pull Up
- Push Down
- Rename Method (changing the name of a method into a new one that better reveals its purpose).
[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] Natural language
The term refactoring originated in computer programming, but the concept has also been applied when modifying any text.
On Wiki websites, refactoring refers to the process of rewriting and reorganizing text to shorten it while preserving content. This particularly applies to discussions, which thus can be made accessible to people who are interested in the arguments made in the discussion, and the information to be gleaned from it, rather than the history of the discussion itself. Refactoring can be difficult to do in such a way that it makes all participants of a discussion happy.
- For the Wikipedia policy on refactoring, please see: Wikipedia:Refactoring talk pages
Most recently, biological engineers have begun exploring the idea of refactoring the genomes of natural biological systems in order to make living organisms easier to understand and interact with.
[edit] Etymology
The first known use of the term "refactoring" in the published literature was in the article, Refactoring: An Aid in Designing Application Frameworks and Evolving Object-Oriented Systems, Proceedings of the Symposium on Object Oriented Programming Emphasizing Practical Applications (SOOPPA) September, 1990, ACM by William F. Opdyke and Ralph E. Johnson [2]. William Opdyke's Ph.D. thesis on "Refactoring Object-Oriented Framework", University of Illinois, was published in 1992 [3]. The term "refactoring" was almost certainly used before then. Refactoring as a technique was definitely used before then.
As a neologism, it is clearly a reference to mathematical factoring.
[edit] References
- 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.. Refactoring Workbook. Addison-Wesley. ISBN 0-321-10929-5.
- Feathers, Michael C. Working Effectively with Legacy Code. Prentice Hall. ISBN 0-13-117705-2.
[edit] See also
- Code review
- Design pattern (computer science)
- Inheritance semantics
- Peer review
- Separation of concerns
- Test-driven development
- Unit testing
- Wikipedia:Refactoring talk pages
[edit] External links
- What Is Refactoring? (c2.com article)
- Martin Fowler's homepage about refactoring
- 1993 PhD dissertation by William F. Opdyke
- Aspect-Oriented Refactoring by Ramnivas Laddad
- A Survey of Software Refactoring by Tom Mens and Tom Tourwé
- How to Write Unmaintainable Code by Roedy Green
- Refactoring Tools
- Refactorings - alphabetical list
- Refactoring To Patterns Catalog
- Citations from CiteSeer
- Refactoring and Software Engineering
- Refactoring Java Code
- General Refactoring links
- Refactoring for Microcontroller Software
- Refuctoring - The Opposite Of Refactoring (from spoof Waterfall2006 conference)
- Articles about refactoring