What is refactoring?

Definition

The goal is to make code easier to understand, maintain or extend without changing what the existing system is supposed to do. Changes can range from reorganizing a function to improving larger structural boundaries.

Preservation needs evidence. Tests, comparisons and review help establish that the new structure still meets the original requirements, including important behavior that may not be obvious from a superficial inspection.

ELI5

Think of reorganizing a workshop so it is easier to use while keeping the same work possible.

For example, a developer can consolidate repeated logic into one helper and check that callers still receive the same results.

Is every rewrite a refactor?

No. A rewrite may also change behavior or requirements, while refactoring specifically aims to preserve the intended behavior.

Why verify existing behavior?

Structural changes can accidentally alter hidden assumptions or edge cases even when the new code looks cleaner.

  1. Denys Linkov beside the headline CODING AGENTS MISS LEGACY RISKS on a pure black background.
Definition card for refactoring: What is refactoring?

Refactoring changes the internal structure of software while preserving its intended externally observable behavior.