Glossary · Programming

Refactoring

Refactoring is restructuring existing code to make it cleaner and easier to maintain without changing what the program does from the outside.

Updated October 6, 2026

Refactoring is the disciplined practice of changing the internal structure of code, such as renaming, splitting a long function or removing duplication, without altering its externally visible behavior.

Why it matters

Code is read far more often than it is written. Regular refactoring keeps a codebase understandable and pays down technical debt, which makes later features cheaper to add. It is only safe when tests confirm that behavior stayed the same.

Example

Before:

def total(items):
    t = 0
    for i in items:
        t = t + i["price"] * i["qty"]
    return t

After:

def line_total(item):
    return item["price"] * item["qty"]

def total(items):
    return sum(line_total(i) for i in items)

The result is the same, but each piece now has a clear name and can be tested separately.

Common mistakes

  • Refactoring without tests, then discovering behavior changed.
  • Mixing a refactor with a new feature in one commit, which makes review and rollback hard.
  • Rewriting everything from scratch when small, incremental steps would do.

Get notified when this term updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.