Code refactoring

Code refactoring is the process of changing a computer program's code to make it amenable to change, improve its readability, or simplify its structure, while preserving its existing functionality.


In software engineering, "refactoring" source code means modifying it without changing its behavior, and is sometimes informally referred to as "cleaning it up". Refactoring neither fixes bugs nor adds new functionality, though it might precede either activity. Rather it improves the understandability of the code and changes its internal structure and design, and removes dead code, to make it easier to comprehend, more maintainable and amenable to change. Refactoring is usually motivated by the difficulty of adding new functionality to a program or fixing a bug in it.

In extreme programming and other agile methodologies, refactoring is an integral part of the software development cycle: developers first write tests, then write code to make the tests pass, and finally refactor the code to improve its internal consistency and clarity. Automatic unit testing ensures that refactoring preserves correctness.

Code smells are a heuristic to indicate when to refactor, and what specific refactoring techniques to use.

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, to ensure that existing behavior is preserved. The new aspect is explicitly wanting to improve an existing design without altering its intent or behavior.

Some refactoring methods face challenges in being used. 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.


Although refactoring code has been done informally for years, William Opdyke's 1993 Ph.D. dissertation 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.

Martin Fowler's book Refactoring: Improving the Design of Existing Code is the canonical reference.

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. Opdyke's Ph.D. thesis, published in 1992, also used this term. The term "refactoring" was almost certainly used before then.

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.

List of refactoring 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.

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.

See also

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.


External links

Search another word or see Code_refactoringon Dictionary | Thesaurus |Spanish
Copyright © 2015, LLC. All rights reserved.
  • Please Login or Sign Up to use the Recent Searches feature