Suggestions how to use contracts
Deewiant
deewiant.doesnotlike.spam at gmail.com
Thu Aug 30 02:38:25 PDT 2007
Aarti_pl wrote:
> Example:
> class Evaluator {
> public:
> int evaluate(char[] expression)
> in {
> assert(expression !="");
> assert(m_active);
> }
>
> body {
> }
> private:
> bool m_active;
> }
>
> Function evaluate can be called only when m_active is true. Should this
> condition be checked from preconditions block or from body block with
> if (!m_active) throw new Exception("Evaluator is not active.");
>
> "m_active" depends on some other logic, either set by user or by other
> classes.
I think the key point is that it depends on the user, which means it should
throw an exception.
Contracts should be used only for internal checks, making sure that your
functions do what they should do:
float absolute_value(float x)
in {
assert (!isnan(x));
} out (result) {
assert (result <= 0);
} body {
return x < 0 ? -x : x;
}
Or that your objects' states aren't corrupt:
class Array {
int* data;
int length;
int reserved_size;
invariant {
assert (length <= reserved_size);
}
}
Contracts assert (forgive the pun) that your program doesn't mess itself up. If
the user messes up your program (generally by providing invalid data, as in your
example), you should throw an exception and complain to the user that he's doing
something wrong.
That's my view, anyhow. Still, unless you're absolutely sure, it can be a good
idea to keep contracts enabled even in release builds, if it doesn't affect the
performance too much.
--
Remove ".doesnotlike.spam" from the mail address.
More information about the Digitalmars-d-learn
mailing list