Wednesday, February 13, 2008

This is a general recommendation if you're planning to use a tool like Pex in the future: make sure that preconditions (i.e. parameter validation) fails in a different fashion that other assertions.

Here's a snippet that shows the problem:

// don't do this
void Clone(ICloneable o) {
     Debug.Assert(o != null); // pre-condition
     ...
     object clone = o.Clone();
     Debug.Assert(clone); // assertion
}

Why is this bad?

A tool like Pex will explore your code and try to trigger every Debug.Assert it finds on its way. When the assertion is a precondition, it is likely expected and one would like to emit a negative test case (i.e. 'expected exception').

The problem in the snippet above is that both failure will yield to the same assertion exception and it will very difficult to *automatically* triage the failure as expected or not.

How do I fix this?

Make sure different classes of assertions can be differentiated automatically, through different exception types, tags in the message, etc...

Thursday, February 14, 2008 2:08:10 AM UTC
Hi Jonathan,

Our team has started to use more Debug assertions (previously we were using exceptions everywhere) at the recommendation of the Code Complete book. We now use assertions whenever something should never happen (e.g. something in the system is terribly wrong), whereas we use exceptions to indicate input errors, invalid order of calls, etc. Will this kind of coding cause us problems in the future, should we ever get our hands on Pex?

I guess I don't understand your recommendations, perhaps not even the problem definition. Can you explain further?
Thursday, February 14, 2008 6:53:40 AM UTC
No, you don't have any problem!

In your case, assertions are used correctly (something that should never happen), while parameter validations throws exceptions. In this case, when Pex generates a test that triggers an exception it will be easy to triage it into the 'expected' or 'unexpected' buckets.

However, some people don't like to pay the price of runtime checks for parameters and use Debug.Assert to do that as well (as in the example), that's where problems occur.
Thursday, February 14, 2008 9:00:24 AM UTC
Gotcha. Thanks for the info. Now get back to coding Pex so we can start using it. ;-)
Comments are closed.