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 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
- “Design the call site first. The component is correct when a reader who never opens its file still predicts its behavior.”
- Every surprise is a deferred debugging session, billed to whoever inherits the code.
- The test is free: read your own call sites a week after writing them.
Follow-ups to expect
- Doesn’t this make names long? Yes, and autocompletion carries the length. Explicit beats cute.
- What about performance hidden behind a simple name? A cache that changes timing is acceptable; one that changes semantics is not.
- How is this different from “be consistent”? Consistency is one input. The rule is effects visible at the call site, and consistency is how you keep them predictable in bulk.
Red flags in an answer
- Names describing implementation:
Manager,Helper,Util,DataProcessor. - Read-named calls that write, send, or block.
- Errors swallowed or silently converted.
- Same verb, different semantics across components.
- “It is all explained in the docs”: the docs are the apology, not the design.
Sources
- API Design Guidelines, Swift.org: the cleanest published formulation of the axis. Clarity at the point of use is the stated most important goal, and names are optimized for the call site, not the declaration site.
- Principle of least astonishment, Wikipedia: the concept’s history back to Raymond and early Unix design lore.
- Rust API Guidelines, The Rust Project: surface-wide consistency rules, the same-verb-same-semantics discipline enforced across a whole ecosystem.