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, x2x − 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].

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

[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

[edit] Further reading

[edit] References

  1. ^ a b Fowler, Martin (1999). Refactoring. Addison-Wesley. ISBN 0-201-48567-2. 
  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.
  3. ^ a b Martin Fowler, "MF Bliki: EtymologyOfRefactoring"
  4. ^ Refactoring techniques in Fowler's refactoring Website
  5. ^ 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