Why no contracts for release build?
Jonathan M Davis via Digitalmars-d
digitalmars-d at puremagic.com
Tue Jun 3 10:13:48 PDT 2014
On Tue, 03 Jun 2014 16:29:20 +0200
Andre via Digitalmars-d <digitalmars-d at puremagic.com> 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
More information about the Digitalmars-d
mailing list