Refactoring: Changing Structure Without Changing Behaviour
Refactoring is a sequence of small edits that each keep the program doing the same thing while moving it toward a shape that is easier to change. The discipline is in the size of the step and in running the tests between steps.
What a learner can do afterwards
- Extract a function from a long block without altering any observable behaviour
- Rename and move a class in steps, keeping the code running throughout
- Say what makes a change a refactoring rather than a rewrite
1 · Read
Refactoring changes structure while behavior stays put. You move in steps so small that each one is obviously safe, and you run the tests between steps. Small steps plus green tests is the whole discipline.
A forty-line function hides three jobs: reading input, computing the bill, and printing it. Pull each job into a named helper, one at a time, testing after each pull. The program still prints the same bill, but each piece now has a name.
Renames and moves work the same way. Rename in one step, move the class in the next, and keep the code running throughout. If a step turns red, undo that one step while the change is still tiny.
A change that fixes a bug or adds a feature is not refactoring, even if it tidies too. A rewrite that swaps the whole block at once is not refactoring either. Same behavior, small steps, tested throughout.
Same behavior, small tested steps, and tidy shape at the end.
2 · Watch
Take it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.