Consistent Ok/Error Spec

View Source

If a -spec declaration includes an {ok, ...} tuple as possible result, it should also include possible error results.

Avoid

-spec some_function(input()) -> {ok, output()}.

Prefer

-spec some_function(input()) -> {ok, output()} | {error, error()}.

or:

-spec some_function(input()) -> output().

Rationale

Wrapping up results in an {ok, …} tuple doesn't serve any purpose other than leading its users to think that the function may also return errors. If that's the case, then those errors should be part of the function spec. If it's not, then it's better for consistency, composition, and refactoring to just return the expected output, without wrapping it up.

Options

  • None.

Example configuration

{elvis_style, consistent_ok_error_spec, #{}}