Checking function parameters in Phobos
Joseph Rushton Wakeling
joseph.wakeling at webdrake.net
Tue Nov 19 23:51:16 PST 2013
On 20/11/13 01:01, Andrei Alexandrescu wrote:
> There's been recent discussion herein about what parameter validation method
> would be best for Phobos to adhere to.
>
> Currently we are using a mix of approaches:
>
> 1. Some functions enforce()
>
> 2. Some functions just assert()
>
> 3. Some (fewer I think) functions assert(0)
>
> 4. Some functions don't do explicit checking, relying instead on lower-level
> enforcement such as null dereference and bounds checking to ensure safety.
>
> Each method has its place. The question is what guidelines we put forward for
> Phobos code to follow; we're a bit loose about that right now.
Regarding enforce() vs. assert(), a good rule that I remember having suggested
to me was that enforce() should be used for actual runtime checking (e.g.
checking that the input to a public API function has correct properties),
assert() should be used to test logical failures (i.e. checking that cases which
should never arise, really don't arise).
I've always followed that as a rule of thumb ever since.
More information about the Digitalmars-d
mailing list