[phobos] Proposal of StopWatch module

Jonathan M Davis jmdavisprog at gmail.com
Tue Aug 17 22:21:52 PDT 2010


On Tuesday 17 August 2010 21:46:27 SHOO wrote:
> > 2. As a user of this module, I would much prefer to have an exception
> > throw when I call start or stop function out of order, instead of silent
> > return. If the only returns, there semantic requirement for having stop
> > function - it is has then same meaning as peek.
> 
> There are three ways:
> 1. enforce
> 2. assert
> 3. return;
> 
> Which do you like?
> I like #2 because I want to suppress influence to runtime.

I think that the typical thing to do at this point is to use assert internally 
to verify the state of the struct or class itself, and to use enforce to check 
input from the users of the module. As for calling stop() out of order, that's a 
bit harder to say. It's not checking input per se, but it's not exactly checking 
the internal consistency of the struct either...

I think that I'm going to have to go with enforce() on this one simply because 
odds are that the users of the module are going to be using the phobos lib which 
will likely have been compiled in release mode where there are no assertions. As 
such, an assert would generally be useless, and you're just going to get silent 
failures. If it were primarily other Phobos modules using it that would be one 
thing, but since it's definitely intended for general use, I'd have to say use 
enforce().

Now, what you can do if you think that that could be a performance issue for a 
high-precision timer is you can have two versions of the function - one which 
has enforce() and one which silently fails on a mismatch. That one, the user can 
decide which to use. So, maybe stop() and checkedStop() or something similar.

- Jonathan M Davis


More information about the phobos mailing list