An error code is a returned value indicating an exceptional condition instead of transferring control by throwing an exception. It keeps failure visible in the function’s result, but callers must detect and handle it. section 4.1 section 4.2
Error codes create several practical tensions:
- They can silently propagate when callers forget to check them, delaying failure and weakening compiler assistance. section 4.2
- They add explicit checking and aggregation boilerplate when several functions can fail. section 4.2
- They fit poorly with polymorphic functions when no sentinel value exists for the result type, such as an empty-sequence maximum. section 4.2
- A typed alternative returns a generic value representing success or failure, then uses higher-order functions to centralize propagation and handling. section 4.1
An undefined mean
A mean function can throw when given an empty sequence, return a caller-supplied fallback, or represent the exceptional case explicitly. The fallback approach makes the function total but forces callers to choose a policy immediately and limits the result to Double. section 4.2
