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.
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.
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.
US Patent Issued to Panasonic on Aug. 14 for "Digital Video Receiver, ECM Extract Equipment, EMM Extract Equipment, Scramble Key Extract Equipment, CCI Extract Equipment, Digital Video Receiving System, Ecm Extract Method, Emm Extract Method, Scramble Key Extract Method, CCI Extract Method, Digital Video Receiving Method, and Recording Medium" (Japanese Inventor)
Aug 21, 2012; ALEXANDRIA, Va., Aug. 21 -- United States Patent no. 8,243,927, issued on Aug. 14, was assigned to Panasonic Corp. (Osaka,...
US Patent Issued to Electronics and Telecommunications Research Institute on Dec. 25 for "Correlation Extract Method for Generating 3d Motion Data, and Motion Capture System and Method for Easy Composition of Humanoid Character on Real Background Image Using the Same" (South Korean Inventors)
Dec 29, 2012; ALEXANDRIA, Va., Dec. 29 -- United States Patent no. 8,340,398, issued on Dec. 25, was assigned to Electronics and...
US Patent Issued to Beijing Gingko-Group Biological Technology on June 26 for "Black Soybean Hull Extract, Method for Obtaining, and Use Thereof" (Chinese Inventors)
Jun 29, 2012; ALEXANDRIA, Va., June 29 -- United States Patent no. 8,206,764, issued on June 26, was assigned to Beijing Gingko-Group...
US Patent Issued to Beijing Gingko-Group Biological Technology on Sept. 4 for "Black Soybean Hull Extract, Method for Obtaining, and Use Thereof" (Chinese Inventors)
Sep 12, 2012; ALEXANDRIA, Va., Sept. 12 -- United States Patent no. 8,257,756, issued on Sept. 4, was assigned to Beijing Gingko-Group...