---
title: "Refactoring: Changing Structure Without Changing Behaviour"
description: "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 runni"
canonical: https://lightmysky.com/learn/computing/refactoring-changing-structure-without-changing-behaviour-mt_HHZufOyTg4
source: https://lightmysky.com/learn/computing/refactoring-changing-structure-without-changing-behaviour-mt_HHZufOyTg4.md
retrieved: 2026-09-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# 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.

Subject: Computing · Area: Software Engineering · Ages 19 to 21
Page: https://lightmysky.com/learn/computing/refactoring-changing-structure-without-changing-behaviour-mt_HHZufOyTg4

## Ready when they can

- 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

## Lesson: Tidying code without breaking it

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.

**Example.** 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.

**Tip.** 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.

**Recap.** Same behavior, small tested steps, and tidy shape at the end.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Design Patterns: Naming a Solution That Keeps Recurring](https://lightmysky.com/learn/computing/design-patterns-naming-a-solution-that-keeps-recurring-mt_bhUEk8PwBp)
- [Unit Tests: Deciding What Is Worth Testing](https://lightmysky.com/learn/computing/unit-tests-deciding-what-is-worth-testing-mt_tuIQecq5Le)

## Opens up

- [Test Doubles and Testing at a Boundary](https://lightmysky.com/learn/computing/test-doubles-and-testing-at-a-boundary-mt_4ep3SiMYNM)
- [Technical Debt and the Cost of the Next Change](https://lightmysky.com/learn/computing/technical-debt-and-the-cost-of-the-next-change-mt_iA8-0Qg4ID)
