Nguyen Le Phong

Interfaces That Outlive the Code Behind Them

How to design an interface that survives rewrites: hide what's likely to change, mind Hyrum's Law, publish late, test the promise, evolve it without breaking callers.

Say your team is sketching a new shipping module. Everyone in the room knows the code behind it will change. The carrier you integrate with today might be replaced next year, the rates table will get reshaped at least twice, and nobody's sure the feature keeps its current name. So the question worth the meeting time is what this module promises everyone else. What the inside looks like can wait.

That promise is the interface. Get it right and you can rewrite the inside three times without the rest of the codebase noticing. Get it wrong and every internal change turns into a negotiation with every caller. Let's look at where good promises come from, why they're always bigger than you think, and how to change one without breaking anyone.

Engineers go through boundary cards and an architecture sketch around a shared office table.
The inside of a module will keep changing. The promise on its edge is what the rest of the code gets to rely on.

Hide what's likely to change

The best advice on this is from 1972. In "On the Criteria To Be Used in Decomposing Systems into Modules", David Parnas suggested you start with a list of difficult design decisions, or decisions likely to change, and design each module to hide one of them from the others (Parnas, Communications of the ACM, 1972). Fifty-odd years later it's still the sharpest test for an interface: what is this hiding, and is that the thing most likely to move?

Here's what not hiding it looks like:

interface ShippingQuotes {
  getCarrierRates(req: CarrierRateRequest): Promise<CarrierRateResponse>;
}

Every caller now speaks the carrier's language. Switch carriers and you touch every screen, job and test that asks for a price. Compare:

type Quote = { carrier: string; price: number; etaDays: number };

interface ShippingQuotes {
  // Empty list when no carrier serves the address. Never throws for that case.
  quotesFor(orderId: string): Promise<Quote[]>;
}

This version names the job in product terms: give me quotes for this order. The carrier, its request format and its quirks all stay inside. Notice the comment, too. Hiding the carrier is good. Hiding the fact that some addresses get no quotes would just make every caller guess. A good interface hides what's likely to change and spells out the behavior callers really need. If you want more on stating limits like that, see Making Interfaces Honest About System Limits.

Where do you find the right promise? Rarely on a blank whiteboard. Watch the changes you're already making: the rule people keep re-explaining in planning, the vendor detail that keeps leaking into screens and tests that shouldn't care. Those leaks are your best design material.

Your contract is bigger than what you wrote

Hyrum Wright named this one while working on core C++ libraries at Google, where even small library changes broke systems far away: "With a sufficient number of users of an API, it does not matter what you promise in the contract: all observable behaviors of your system will be depended on by somebody" (Hyrum's Law). Response time, the order of items in a list, the exact text of an error message: if callers can see it, someone will build on it.

So for each observable behavior, make a choice. Either promise it out loud, with a doc line and a test, or make sure nobody can lean on it by accident. If quotesFor happens to return the cheapest quote first today, decide now whether that's a promise. If it is, write it down. If it isn't, don't be surprised when a screen breaks the day the sort order changes.

Be stingy about what you publish

Martin Fowler draws a line that matters more than public versus private: a published interface is one used outside the code base it's defined in (PublishedInterface). Inside one repo, you can rename a method and fix every caller in the same commit. Once another team, a mobile app already in users' hands or a partner depends on it, you can't. Every change needs a migration plan.

That's a good reason to publish later than feels natural. Keep a module's interface internal until the shape has survived a few real changes, then publish the smallest version that does the job. It's much easier to add a field later than to take one back.

Test the promise in product language

A test around an interface shouldn't only prove today's implementation works. It should describe what any future implementation must keep doing. Compare a test called "calls carrier API with correct payload" with one called "an order to an unsupported area gets no quotes and no error". The first breaks the day you switch carriers. The second still holds, and tells the next implementer exactly what to preserve.

When the callers live in other teams, let them write some of those tests. Ian Robinson described this in 2006 as consumer-driven contracts: each consumer writes its expectations as tests, and the provider runs all of them on every build, so a breaking change shows up before it ships (Consumer-Driven Contracts). The same article suggests consumers validate only the data they actually use, which keeps them from breaking when the provider adds something new.

Changing an interface without breaking anyone

Sooner or later the promise itself has to change. Google's API guide AIP-180 is a handy checklist of what counts as breaking (AIP-180: Backwards compatibility):

Change Safe in the same major version?
Add a new optional field or method Yes
Add a new required field to an existing request No
Remove or rename a field No (a rename is a remove plus an add)
Change a default value No
Change visible behavior that reasonable callers rely on No

When you do need a breaking change, the boring sequence works: add the new shape next to the old one, mark the old one deprecated with a date and a migration note, move callers over, watch usage drop to zero, then delete. It's slower than editing in place, and every caller will thank you for it.

Try this week

  • Pick one module and write down which decision it hides. If you can't name one, its interface is probably exposing the implementation.
  • Search callers for a vendor or table name that leaked through an interface, and list what would break if that vendor changed.
  • Choose one behavior callers can observe but you never promised (sort order, an error message) and decide: promise it or protect it.
  • Rename one implementation-flavored test so it describes product behavior instead.

For a worked example of keeping vendors behind an interface, read Ports and Adapters, Explained With One Checkout.

What did you think?

Frequently asked questions

What makes a software interface durable?
It hides the decisions most likely to change, such as which vendor, table or algorithm sits behind it, and states in product language the behavior callers really depend on. David Parnas proposed this in 1972: list the design decisions likely to change and design each module to hide one of them.
What is Hyrum's Law?
An observation by Hyrum Wright from his time working on core libraries at Google: with enough users of an API, every observable behavior will be depended on by somebody, whatever the contract says. Response time, sort order and error text all become part of the real contract, so decide which ones to promise and test.
What is the difference between a public and a published interface?
Martin Fowler defines a published interface as one used outside the code base it's defined in. A public method inside one repo can be renamed along with all its callers in one commit. A published interface can't, so every change needs a migration plan.
Which interface changes are breaking?
Google's AIP-180 treats these as breaking within a major version: adding a required field to an existing request, removing or renaming a field, changing a default value, and changing visible behavior that reasonable callers rely on. Adding new optional fields or methods is safe.
How do you test an interface rather than an implementation?
Name and write tests in product terms, describing behavior any future implementation must keep, such as an unsupported address getting no quotes and no error. When other teams consume the interface, consumer-driven contract tests let each consumer encode its expectations and the provider run them on every build.