Refactoring
Also known as: Code refactoring
Process of restructuring existing code to improve its design, readability or maintainability without changing its external behavior.
Refactoring means changing the internal structure of code while keeping what it does exactly the same. Typical refactorings include renaming a variable to something clearer, extracting a block of code into its own well-named function, splitting a large class into smaller ones, removing duplication and replacing complicated conditionals with simpler constructs. Each step is small and preserves behavior, and many small steps can add up to a substantial redesign. The aim is code that is easier to understand and cheaper to change when features or fixes are needed.
William Opdyke's 1992 doctoral thesis at the University of Illinois studied the refactoring of object-oriented programs, and Martin Fowler's 1999 book "Refactoring: Improving the Design of Existing Code" popularized the term. It cataloged named refactorings and the code smells, warning signs such as long methods and duplicated logic, that suggest them. Refactoring was integral to Kent Beck's Extreme Programming and is the third step of the red, green, refactor cycle in Test-Driven development. Development environments automate many refactorings, such as renaming a function and safely updating every place it is called.
Refactoring is the main remedy for technical debt, gradually improving structure without halting development for a risky rewrite. It depends on a safety net: comprehensive unit tests give confidence that behavior has not changed. Refactoring without tests, or mixing it with functional changes in the same commit, makes problems hard to trace. Teams sometimes struggle to justify refactoring because its benefits are indirect, so many practitioners favor continuous small improvements, tidying code while working on it, over large dedicated clean-up projects.
Key points
- Refactoring improves code structure through small steps that leave external behavior unchanged.
- Martin Fowler's 1999 book named and cataloged common refactorings and the code smells that prompt them.
- Automated tests provide the safety net that makes refactoring low-risk.
- Regular refactoring is the main way teams pay down technical debt without a full rewrite.
Related entries
Technical Debt
Metaphor for the future cost of rework created when software teams choose a quick or expedient solution instead of a cleaner, more durable one.
Test-Driven Development
Software development practice in which a failing automated test is written before the code that makes it pass, after which the code is refined.
Unit Testing
Practice of verifying that small, individual pieces of code, such as single functions or classes, behave correctly when tested in isolation.
Martin Fowler
British software developer and author known for popularizing refactoring, enterprise design patterns, microservices and agile development practices.
Kent Beck
American software engineer who created Extreme Programming, popularized test-driven development and co-wrote the Manifesto for Agile Software Development.
Code Review
Practice in which developers examine each other's source code changes to find defects, share knowledge and keep the codebase consistent.
Don't Repeat Yourself
Software development principle that every piece of knowledge in a system should have a single, authoritative representation rather than duplicates.
Design Pattern
Reusable, named solution to a commonly recurring problem in software design, described as a template rather than as finished code.
Featured in guides
Referenced by
Other entries that discuss or point to Refactoring.