Table of Contents
Principle Of Least Surprise (PLS)
Variants and Alternative Names
- Principle of Least Astonishment (PLA)
- May also be referred to as “rule” or “law” instead of “principle”
- Acronyms sometimes include the “o” for “of”: PoLA, PoLS
In interface design, always do the least surprising thing.1)
Never surprise the user. An interface should behave exactly as the user thinks it behaves. What surprises the user depends on the kind of interface (user interface, module interface) and the type of user (end user, fellow programmer, maintainer). The central idea of PLS is to think about how the user would want to use the interface.
Surprises are always a potential source for frustration. A user wants to be in control of the system. If the system does not behave as intended, the user gets disappointed and has to determine how to get the system do what it should do. On the other hand a system that behaves according to the users wishes is pleasant to use.
Secondly when everything works as expected, the user will make fewer mistakes. In case of a user interface this means that the user is more effective and in case of a module interface the software will have fewer defects.
- Separate methods that change an object (commands) from methods asking the object a question (queries) (see CQS)
- This especially means that a query method should not alter the observable object state
- Name all modules in a way that clearly communicates what the module is and does
- Names of classes shall be nouns representing a specific (real-world) concept (see MP)
- Names of
interfacesshall be adjectives describing a specific property. This typically results in names ending with -able
- Names of command methods shall be verbs (in imperative form)
- Names of query methods shall start with get- or is-
- Names of mathematical functions or the like shall be named by the respective concept (like
- Avoid “clever” solutions which are hard to grasp in favor of simple, dumb ones (see KISS)
- Tend to use the first solution that comes in mind
See section contrary principles.
The precise origin is unknown. Probably it's The Tao Of Programming by Geoffrey James.
Accepted: PLA is widely known and also treated in Eric S. Raymond's The Art of Unix Programming
Relations to Other Principles
- Easy to Use and Hard to Misuse (EUHM): A module is easy to use if there is no surprise in how it works.
- Command-Query Separation (CQS)
- Fail Fast (FF): FF is about what a module should do in the case of error. PLS on the other hand is about how the module should behave normally. Furthermore it normally is not a surprise that a module fails when there is an error but a module that doesn't fail when it should, behaves strangely.
- Model Principle (MP): PLS is mainly about how module identifier and module behavior relate to each other. MP tells that modules named according to the model are least surprising.
- Uniformity Principle (UP): When applying PLS, UP should also be considered for naming modules.
- Eric S. Raymond: The Art of Unix Programming: Rule of Least Surprise
- Eric S. Raymond: The Art of Unix Programming: Applying The Rule of Least Surprise
- Joshua Bloch: How to Design a Good API & Why it Matters
Discuss this wiki article and the principle on the corresponding talk page.