Refactoring
Refactoring is a disciplined technique for restructuring existing code, altering its internal structure without changing its external behavior. You apply a series of small behavior-preserving transformations and run the tests after each one, keeping the code fully working while its design improves.
itSoftware engineering | OpenSkills.info
Course pathWalk it in order
Look it upDip in anytime
Go furtherLeaves this page
Intro
Refactoring
Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior-preserving transformations. Each transformation, called a refactoring, does little by itself, but a sequence of them can produce a significant restructuring. Because each step is small, it is less likely to go wrong. The system stays fully working after every step, which reduces the chance of a serious break during the restructuring.
Fowler's book defines the term twice. As a noun, a refactoring is a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior. As a verb, to refactor is to restructure software by applying a series of refactorings without changing its observable behavior. The word is not a synonym for cleaning up code; restructuring is the general term, and refactoring is one specific technique inside it.
Behavior preservation
External behavior is what a caller observes: return values, side effects, error behavior, and performance that matters. Refactoring does not alter those. It changes how the code is organized, named, and decomposed.
Behavior preservation is the boundary that separates refactoring from other changes. When you add a feature, you change behavior. When you fix a bug, you change behavior. When you refactor, you keep behavior constant and improve structure. Mixing the two makes regressions hard to attribute, because a failure can no longer be traced to one kind of change.
The practical tool for checking preservation is a test suite. Refactoring without tests is guesswork about whether behavior survived. Fowler describes automated tools as valuable but not essential; small steps and frequent testing are what keep the technique safe.
Code smells
A code smell is an indicator of a problem that refactoring can address. Smells are easy to spot and easy to fix, but they may be symptoms of a deeper problem in the code. The smell is the signal; the refactoring is the response.
Continue the course
This section is part of the paid course.
See pricing to subscribe, or log in if you already have access.
Where this skill leads
Relevant careers
See how this topic contributes to broader role-level skill maps.
Sources
- https://refactoring.com/
Supports
- Definition of refactoring as a noun and a verb
- Small behavior-preserving transformations and the fully working system
- Tests as the practical tool for checking behavior preservation
- https://refactoring.com/catalog/
Supports
- The named inventory of transformations with motivation, mechanics, and example
- Compose-and-split, move, data, and logic refactoring groups
- When each transformation applies (e.g., Replace Conditional with Polymorphism fitting a stable type code)
- https://martinfowler.com/books/refactoring.html
Supports
- The book that codified the discipline in 1999
- Refactoring as cheaper to modify and easier to understand without changing observable behavior
- Refactoring around features and the distinction from rewriting
- https://refactoring.guru/refactoring
Supports
- Systematic explanation of refactoring, techniques, and when to apply them
- Behavior-preservation and test-suite safety framing
- https://refactoring.guru/refactoring/how-to
Supports
- The checklist of refactoring done right: cleaner code, no new functionality, all tests pass
- Small steps and what to do when tests break
- When a rewrite may be the honest option
- https://refactoring.guru/refactoring/when
Supports
- The rule of three
- Refactoring around adding features, fixing bugs, and code review
- https://refactoring.guru/refactoring/smells
Supports
- Code smells grouped as bloaters, change preventers, dispensables, and couplers
- Smell-to-refactoring mapping used in the cheatsheet
- https://martinfowler.com/bliki/EtymologyOfRefactoring.html
Supports
- The factoring metaphor from mathematics
- The Software Refactory conversation between Opdyke and Johnson
- First printed mention in Thinking Forth (1984) and independent development in Forth and Smalltalk communities
- First published literature in a 1990 article by Opdyke and Johnson
- https://en.wikipedia.org/wiki/Code_refactoring
Supports
- The 1990 Opdyke and Johnson article and the 1991 and 1992 dissertations
- Codification of the discipline by Fowler's 1999 book
- Technique and tool support history across IDEs
- https://www.goodreads.com/book/show/44919.Working_Effectively_with_Legacy_Code
Supports
- Legacy code defined as code without tests
- Characterization testing as the baseline for untested code
- https://michaelfeathers.silvrback.com/characterization-testing
Supports
- Characterization tests documenting current behavior as a baseline, not an endorsement
- Using the baseline so a behavior-preserving transformation can be distinguished from a behavior change
- https://www.jetbrains.com/help/idea/refactoring-source-code.html
Supports
- IntelliJ IDEA rename, extract, inline, move, and change-signature operations with previews
- https://en.wikipedia.org/wiki/IntelliJ_IDEA
Supports
- IntelliJ IDEA first released in January 2001 as a mainstream IDE built around integrated refactoring
- Timeline event for IntelliJ IDEA bringing refactoring to the IDE
- https://learn.microsoft.com/en-us/visualstudio/ide/refactoring-code-generation-quick-actions
Supports
- Visual Studio refactorings and quick actions for .NET and C++
- https://help.eclipse.org/latest/topic/org.eclipse.jdt.doc.user/concepts/crefactor.htm
Supports
- Eclipse JDT refactorings for Java
- https://github.com/facebook/jscodeshift
Supports
- AST-based codemod toolkit for JavaScript transformations
- https://docs.openrewrite.org/
Supports
- Automated refactoring engine with prepackaged recipes for framework migrations, security fixes, and style
- https://comby.dev/
Supports
- Structural search and replace matching code by structure rather than text
