Am 03.06.2014 19:13, schrieb Jonathan M Davis via Digitalmars-d:
On Tue, 03 Jun 2014 16:29:20 +0200
Andre via Digitalmars-d <[email protected]> wrote:

Hi,
I currently think about whether or not contracts should be available
in release builds.

Assuming a small example, you have an account class with a deposit
method, the input parameter must always be > 0.

void deposit(int amount)
in
{
    enforce(amount > 0);
}
body
{
    this.amount += amount;
}

If in release build the Contracts sections is removed, this means, I
also need to add in addition in another place the enforce method.
Otherwise in the productive scenario, this coding isn't secure
anymore. This leads to code duplication.

I think Contracts are not a feature solely for unittests, they are a
fundamental part of classes/interfaces and theirfore should be
available in all builds.

What do you think?

Contracts are specifically intended for validating the input and output
of a function with assertions. So, they're going to go away in any
build that does not have assertions enabled. Contracts are very much
the wrong place to use enforce. enforce throws an Exception and does
not get compiled out. It is intended for error-handling rather than for
validating the correctness of your code.

- Jonathan M Davis


As I known that assertions are only for unittest purposes I hoped,
if I use enforce, this statements will not be removed in release build
and will still be executed - wrong.

I see, the idea of contracts in D differs to the idea of contracts e.g.
in Java (http://c4j-team.github.io/C4J/
Here contracts are used for unittest but also for the productive code
in release builds.

I thought about writing a Contracts library, which works similiar to C4J
but I see no chance to solve this issue without AST.

Kind regards
André

Reply via email to