Q3 · How do you apply the Principle of Least Surprises when designing components?

Back to 24 Architecture Questions.

The question is not about being polite in your API. The axis it turns on: whether a competent reader can predict the call from its name and signature alone. The test is cheap: hand a teammate the call site and nothing else. Whatever their prediction misses is a surprise, no matter what the documentation says.

What the signature cannot tell you A reader calls getUser by its name and predicts a User object. The call also writes the cache, writes an audit record, and fires a webhook: three effects invisible from the call site. FIGURE 3 WHAT THE SIGNATURE CANNOT TELL YOU Reader reads name, predicts getUser getUser(id) : User User the predicted part hidden from the call site Cache write a write on a read Audit log second write Webhook network call the surprise is exactly the work the signature cannot carry

What the answer should contain

Name carries the verb. Name a component by what it does, not what it is: syncUserCache, not UserManager. A read-shaped name on a call that writes is a lie with good intentions.

Signature carries the contract. Arguments in the order a reader would recite them, a return type that states the outcome, and errors that are visible in the signature (typed exceptions, a result type), not thrown from three layers below.

No hidden effects. If the call writes, sends, or blocks for seconds, the name must say so, or the call must be split. The figure is the failure mode: one predicted return, three invisible effects.

Defaults are the most-read line of the API. A surprising default is multiplied by every call site that does not override it. Pick the default the 90% case would have written out loud.

Same verb, same semantics, everywhere. If get throws “not found” in one component and returns null in another, the reader is surprised once per component. Consistency across the surface is cheaper than cleverness inside one call.

Discipline matters most at boundaries. In-process, a teammate can always open the file and check. At a published API they cannot, so least-surprise discipline matters most exactly where the reader is blind.

Ideas worth mentioning out loud

Follow-ups to expect

Red flags in an answer

Sources