The Principle of Least Surprise is a concept that is used to frame your mindset as you code.
If a necessary feature has a high astonishment factor, it may be necessary to redesign the feature.
This principle is typically used for designers, to guide how they craft functionality (in the sense that the user should be able to reasonably correctly guess the result of an action), but it’s really useful for developers too.
The idea is that code should be organized according to where the reader will expect it to be, and it should operate as one would expect.
myFunc(arg1, null, arg2)
), surprising return values (ie. getTime()
returning a boolean), or unusual defaults. The best example of surprising operation is JavaScript’s parseInt
function: until recently, it operated on base 8 rather than the expected base 10. This continued to cause so many errors to the point where JavaScript actually changed the default to 10 in ES5.