unrust

Skill guide

How do you read code against its contract?

Published

Quick answer

Reading code against its contract means comparing what the code actually does with the behavior its caller, test, type, docstring, or product rule requires. You do not start by asking whether the code looks elegant. You identify the inputs, the promised output, the boundary cases, and the states that must be rejected. Then you trace whether each branch preserves that promise. This is one of the most direct ways to catch code that is internally tidy but functionally wrong.

Find the contract before judging the implementation

Contracts appear in more places than docstrings. A type signature, a failing test, an API specification, a UI rule, or the name of a function can all constrain what the code should do. Start by putting that promise into a short sentence you can test.

If the promise is vague, that is useful information too. You cannot prove a function correct against an intention nobody has made explicit.

Boundary cases expose the missing condition

The happy path often makes incorrect code look correct. Ask what happens at zero, at the exact limit, just past the limit, on an empty collection, or after a prior operation changes state. Those cases force the implementation to reveal its real rule.

For a withdrawal limit, for example, checking whether money has already been spent is different from checking whether the next withdrawal pushes the total over the limit. The prose contract makes that distinction visible.

Review generated code with the same discipline

A suggested implementation can have good naming, sensible comments, and a subtle mismatch with the requirement. The source does not matter. Treat every change as a proposal and compare it with the contract before you trust the style.

This is also a practical review habit for teams. A reviewer who restates the contract and tests its edges gives more useful feedback than a reviewer who only debates formatting.

A small practice loop

  1. 01Choose a small function and write its promise in one sentence before reading the body.
  2. 02List an ordinary case and two boundary cases that the promise must handle.
  3. 03Trace each case through the branches and write a test for the first mismatch you find.

Common questions

What counts as a code contract?

A contract is any reliable statement of expected behavior: a type, test, API specification, docstring, product requirement, or documented invariant. The most useful contracts make inputs, outputs, constraints, and error behavior clear.

Why can clean code still violate its contract?

Style and structure can be tidy while a condition is missing, reversed, or applied at the wrong time. Reading against the expected behavior checks correctness rather than appearance.

Check your own starting point

Unrust uses short, timed, unassisted drills to give developers a per-skill snapshot. One session cannot explain the cause of a result, but it can show you what is worth practicing next.

Take the free diagnostic