Simplifying code without changing its behaviour

How to make code that works but is hard to read clearer: Chesterton's fence, the rule of three, small steps with green tests, and when not to touch.

Methodologymetodologiarefactoringqualitàingegneria del software19 November 2026 · 3 min read

Code that works but that nobody wants to touch is a debt paid at every change. Simplifying it means making it clearer without changing its behaviour: same result, less effort for whoever reads it next.

First rule: behaviour stays identical

A simplification that changes behaviour is a change, and must be treated as one (with tests, review, a description). So you work with green tests, in small steps, and you try every step. If there are no tests, you first write the ones that pin down the current behaviour.

Understand before you touch

Faced with a piece of code that seems useless or odd, Chesterton's fence applies: before removing a fence, you find out why somebody put it there. You read the history of the change, look for who uses it, try to understand the case it protects. If after the examination you find no reason, you remove it; if you do find one, you document it with a comment.

What to look for

  • Deep nesting. A cascade of conditions flattens out with early exits.
  • Long functions with several responsibilities. Split them into functions with names that describe what they do.
  • Unclear names. A name that needs a comment to be understood is the wrong name.
  • Duplication. But you extract a common element only after the third case (the rule of three): two similarities may be a coincidence, and a premature abstraction costs more than the duplication.
  • Dead code and flags that are never used. Remove them.
  • Accidental complexity. Generic structures built for a future that has not arrived.
  • Comments that repeat the code. Remove them; keep the ones that explain why.

How to proceed

One simplification at a time; tests after each; one commit for each with a message that says "no change in behaviour". Whoever reviews must be able to check in a few minutes that nothing has changed.

When not to touch

  • Code you do not understand and that has no tests: write the tests first.
  • Code that is about to be replaced or removed.
  • Performance-critical code, where the "ugly" version is that way for a measured reason.
  • When the simplification is a pretext for rewriting everything: the risk outweighs the benefit.

Warning signs

  • Changing behaviour together with structure in the same step.
  • Removing something without understanding why it was there.
  • Extracting an abstraction after only two uses.
  • A "clean-up" that touches dozens of files without tests.
  • A simplification that makes the code shorter but more obscure.

An example

A function with four nested if statements checks an order. With green tests, each condition is turned into an early exit ("if the order is cancelled, return"), the compound condition gets a name ("order_is_editable"), and an unreachable branch is removed after checking it has no uses. Behaviour is identical and the function reads from top to bottom.

When to use it

When refactoring for clarity without changing behaviour, when code works but is harder to read, maintain or extend than it should be, or when a review shows complexity that has built up for no reason.

This method derives from the open-source agent-skills family by Addy Osmani (MIT licence), rewritten and adapted.

This article is an OIAR skill

What you have just read is the content of a skill from the OIAR catalogue, written for people. Knowledge like this, but richer and deeper, written for the agents, drives the interviews, the analysis and the report inside the platform. Skills are used in OIAR: to try this one on a project of yours you need an account.

code-simplificationA method for simplifying working code without changing its behaviour: tests as a safety net, Chesterton's fence, the rule of three, small steps, when not to touch. Use it when code works but is hard to read, maintain or extend.