# How do you read code against its contract?

> Learn how to compare implementation with intent, find mismatched assumptions, and catch bugs that clean-looking code can hide.

- Canonical HTML: [https://unrust.dev/guides/reading-code-against-a-contract](https://unrust.dev/guides/reading-code-against-a-contract)
- Markdown representation: [https://unrust.dev/guides/reading-code-against-a-contract/index.md](https://unrust.dev/guides/reading-code-against-a-contract/index.md)

Published: 2026-07-13

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

## Practice loop

1. Choose a small function and write its promise in one sentence before reading the body.
2. List an ordinary case and two boundary cases that the promise must handle.
3. Trace 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.

## Related guides

- [What is code tracing?](https://unrust.dev/guides/code-tracing/index.md): Code tracing is the habit of simulating execution step by step. Learn how to trace values, references, loops, and branches without running the program.
- [How do you review AI-generated code?](https://unrust.dev/guides/reviewing-ai-generated-code/index.md): A practical review checklist for AI-generated code: verify the contract, inspect boundaries, run focused tests, and understand every change before shipping.
- [How do you review an access-control code change safely?](https://unrust.dev/guides/reviewing-access-control-changes/index.md): Review access-control changes by mapping the subject, action, resource, context, enforcement point, and denied-path evidence before approval.

Next step: [Take the free diagnostic](https://unrust.dev/diagnostic/index.md)
