assert vs. enforce in Phobos code

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Sun Feb 23 09:51:26 PST 2014


On 23/02/2014 17:10, monarch_dodra wrote:
> As a rule of thumb, "enforce" is not necessarily for things "user-input" but for
> things "outside the programmer's control" eg: "things that can legitimately
> fail", Notably, IO, threads, databases etc...

It seems to me that "enforce" is used extensively in Phobos for other purposes 
than that, and quite often seems to be used for obligatory runtime checks.

> If you see any phobos code that validates the range of its inputs via an
> enforce, please knock yourself out and assert it/contract it.

Well, these are some examples where it seems to me both understandable that 
someone wanted to really, really make sure the values were correct, and at the 
same time to fall outside the scope of "things that can legitimately fail":

https://github.com/D-Programming-Language/phobos/blob/master/std/random.d#L359-L360

https://github.com/D-Programming-Language/phobos/blob/master/std/random.d#L1253-L1255

https://github.com/D-Programming-Language/phobos/blob/master/std/random.d#L1334-L1335

https://github.com/D-Programming-Language/phobos/blob/master/std/random.d#L1345-L1347

https://github.com/D-Programming-Language/phobos/blob/master/std/random.d#L1357-L1359

https://github.com/D-Programming-Language/phobos/blob/master/std/random.d#L1629

https://github.com/D-Programming-Language/phobos/blob/master/std/random.d#L1703

https://github.com/D-Programming-Language/phobos/blob/master/std/random.d#L2061-L2070

I referred to checking user input because the only rationale I can see for these 
statements being enforce is if the values being checked were being treated as 
user input and therefore something that could "legitimately fail".

Or have I missed something about when "enforce" is appropriate? ;-)

Assuming I'm right about these enforce statements being inappropriate, I'll make 
a patch.


More information about the Digitalmars-d mailing list