In software engineering, “refactoring” source code means improving it without changing its overall results, 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 helps to preserve correctness of the refactored code.
Code smells are a heuristic to indicate when to refactor, and what specific refactoring techniques to use.
The simplest example of a refactoring is to change an identifier (such as a variable name) into something more meaningful, such as from a single letter ‘i’ to ‘interestRate’. While the concept of renaming an identifier is relatively simple, the implementation of it may not be. Performing a global search-and-replace operation on any body of text can result in undesired results if by identifiers that contain the original identifier as a substring, overloaded identifiers and scoping rules. A more complex refactoring is to turn the code within a block into a subroutine. An even more complex refactoring is to replace a conditional statement 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, in conjunction with 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. Failure to perform refactoring can result in accumulating technical debt.
Some refactoring methods face challenges in being used.[1] 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.