open encyclopedia * Article Search: * *
*
*

Refactoring

From open-encyclopedia.com - the free encyclopedia.

Refactoring is the process of rewriting written material to improve its readability or structure, with the explicit purpose of keeping its meaning or behavior.

Contents

Refactoring code

In software engineering, the term refactoring is often used to describe modifying source code without changing its external behavior, and is sometimes informally referred to as "cleaning it up". Refactoring is often practiced as 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. Testing ensures that refactoring does not change the behavior of the code.

Refactoring is the part of code maintenance which doesn't fix bugs or add new functionality. Rather it is designed to improve the understandability of the code or change its structure and design, 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.

The term is by analogy with the factorization of numbers and polynomials. For example, x2 − 1 can be factored as (x + 1)(x − 1), revealing an internal structure that was previously not visible (such as the two roots at +1 and -1). Similarly, in software refactoring, the change in visible structure can often reveal the "hidden" internal structure of the original code.

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.

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'. 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.

Refactoring is an important aspect of extreme programming.

Martin Fowler's book Refactoring is the classic reference. Although refactoring code has been done informally for years, William F. Opdyke's 1993 PhD dissertation is the first known paper to specifically examine refactoring. 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 such an important concept that it has been identified as one of the most important software innovations by author and commentator David A. Wheeler.

Many refactorings are meant to cure the ills listed in [5].

Break down large functions

Functions that grow in use also grow in size. After a certain point, it becomes clear that functions can benefit from using functions themselves. Your eyes probe the function looking for a likely candidate to relocate elsewhere. This candidate is chosen for being the largest stretch of code that really has nothing to do with the enclosing function. This lets us easily think up a name that explains what the new function does without having to resort to something contrived just to distinguish it from the original function.

This section is originally from Perl Design Patterns Book

Refactoring other text

The term refactoring originated in computer programming, but the concept has also been applied when modifying any text.

To refactor text, in a WikiWiki, is to rewrite and often to reorganize the text completely or radically, sometimes dramatically shortening it, but in a way that preserves all the important, relevant content. In a WikiWiki, it is usually discussions that are refactored--with the purpose of making the discussion accessible to someone who is 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 is a critically important process in developing and maintaining the Wikipedia.

Obviously, refactoring is difficult to do in such a way that will make all participants in a discussion happy. It requires intelligence, tact, excellent reading comprehension skills, good writing skills, and a lack of bias.

Several good discussions are found at the WikiWeb at c2.com.


The term 'factoring' has been used in the Forth community since at least the early 1980s. Chapter Six of Brodie's book "Thinking Forth" (1984) is dedicated to the subject.

In Forth, factoring has essentially the same meaning that refactoring does in Extreme Programming -- to break down a function (a 'word' in Forth) into smaller, more easily maintained functions.


For policy on refactoring in Wikipedia, see editing policy. In the context of Wikipedia, usually talk pages are refactored, but occasionally, when an article has been developed in a particularly haphazard way, it needs to be changed so radically that the resulting change can be called a refactoring.

Bibliography

  • Martin Fowler: Refactoring. Improving the Design of Existing Code, Addison-Wesley Professional 1999, ISBN 0201485672
  • William C. Wake: Refactoring Workbook, Addison-Wesley, ISBN 0-321-10929-5
  • Joshua Kerievesky: Refactoring To Patterns, Addison-Wesley, ISBN 0321213351

See also

External links

  1. What Is Refactoring? (c2.com article)
  2. Martin Fowler's homepage about refactoring
  3. 1993 PhD dissertation by William F. Opdyke
  4. Aspect-Oriented Refactoring by Ramnivas Laddad
  5. A Survey of Software Refactoring by Tom Mens and Tom Tourwé
  6. How to Write Unmaintainable Code by Roedy Green
  7. Refactoring Tools
  8. Refactorings - alphabetical list
  9. Refactoring To Patterns Catalog
  10. Citations from CiteSeer
  11. Refactoring Java Code

de:Refactoring fr:Refactorisation nl:Refactoring ja:リファクタリング (プログラミング) pl:Refactoring zh-cn:软件重构 zh-tw:軟體重構

Contribute Found an omission? You can freely contribute to this Wikipedia article. Edit Article
Copyright © 2003-2004 Zeeshan Muhammad. All rights reserved. Legal notices. Part of the New Frontier Information Network.