[erlang-questions] "Design By Contract" in Erlang
Jesper Louis Andersen
jesper.louis.andersen@REDACTED
Sun Jan 22 13:33:19 CET 2012
On 1/11/12 10:19 PM, Zabrane Mickael wrote:
> Hi guys,
>
> Any Erlang library to simply *DbC* (à la Eiffel)?
>
I tend to just do:
foo(Args) when GuardArgs ->
true = assert_precondition(Args),
Result = ...
true = assert_postcondition(Result),
Result.
Where GuardArgs protects the inbound arguments as much as possible. The
basic idea is that if your assumptions are not satisfied, you crash
before you begin doing bad stuff. As long as your functions are pure,
this works well. Also note that in many cases, providing such
pre/post-conditions actually strengthens the dialyzer quite a bit since
it uses the information to narrow down the valid control-flow/data-flow
paths in the code.
That said, I am not consistent in writing code like this. I tend to
assert certain situations now and then, especially if the code has
proven to be tricky. In fact, if you look at typical erlang code,
{ok, R} = ...
is such an assertion. It protects the caller by asserting that a certain
operation succeeded. I'll recommend code be built around this and then
handling the crashes properly.
--
Jesper Louis Andersen
Erlang Solutions Ltd., Copenhagen, DK
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120122/6e2161cb/attachment.htm>
More information about the erlang-questions
mailing list