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
Don't Panic
Don't Panic: Refactoring
Refactoring is the practice of changing a program's internal structure while keeping its observable behavior the same. That sounds like asking a building crew to move every wall without disturbing the kettle. The trick is not confidence or an expensive editor. It is a sequence of small transformations, each checked before the next one arrives with a hammer.
The first important distinction is between behavior and structure. Return values, side effects, error behavior, and performance that matters are the contract. Names, functions, classes, and the route data takes through them are the craft. Adding a feature or fixing a bug changes the contract. Refactoring does not. Mixing both jobs in one change makes a failing test a tiny detective novel with no final chapter.
The second idea is the code smell, a visible shape that suggests a design problem might be nearby. A long method, duplicated block, or expanding type-code switch does not command a particular remedy. It gives you a place to investigate. The catalog then offers named transformations such as Extract Function, Move Function, or Replace Conditional with Polymorphism. It is a map of possible moves, not a vending machine that dispenses architecture.
The third idea is the green suite, the state where every test passes. Make one structural change. Run the tests. If they fail, the last step is still close enough to remember without archaeological equipment. For code without tests, write a characterization test first: a test that records what the code does today. It does not certify that the behavior is desirable. It gives you a baseline, which is far more useful than optimism wearing a lanyard.
Read the Intro for the boundary between refactoring, features, fixes, and rewrites. Use the Cheatsheet when a smell needs a candidate transformation and the Practice Reference when you want a compact, test-backed loop to follow. The quiz checks whether those categories have stayed separate in your head. That is the whole arrangement: behavior is the contract, structure is the craft, and small green steps keep the two from becoming an expensive argument.
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
- https://nodejs.org/api/test.html
Supports
- The stable built-in node:test runner and its passing and failing test behavior
- Running deterministic local JavaScript assertions for the practice exercise
- https://martinfowler.com/articles/refactoring-external-service.html
Supports
- Test doubles isolating changing external-service data before refactoring
- The practical boundary between a passing test and deterministic evidence
- https://martinfowler.com/bliki/CodeSmell.html
Supports
- Code smells as surface indications rather than automatic diagnoses or prescriptions
- https://martinfowler.com/bliki/OpportunisticRefactoring.html
Supports
- Feature-branch and code-ownership friction for opportunistic refactoring
- Semantic conflicts as a limit of branch and merge tooling
- https://martinfowler.com/articles/preparatory-refactoring-example.html
Supports
- Creating tests instead of manually inspecting program output to establish a refactoring safety net
- https://engineering.atspotify.com/2023/5/fleet-management-at-spotify-part-3-fleet-wide-refactoring
Supports
- Previewing fleet-wide diffs and testing affected repositories before pull requests
- Gradual rollout where pre-merge tests cannot cover runtime interactions
