views:

97

answers:

5

I've recently listened to Kevin Hazzard talk about Code Contracts in .Net Rocks show 570 (http://devjourney.com/community/dotnet-rocks-show-570-with-kevin-hazzard/). He mentions enabling Run-time Contract Checking as an option some people might choose to use while others might not.

Why would you not use Run-time Contract Checking for your Code Contracts? Is there a significant negative impact on performance? Other reasons?

If you disable this feature, how do you deal with preconditions in your methods at run-time?

+1  A: 

It would seem off to me to use both Contracts and another form of precondition checking - as you'd presumably then end up duplicating your preconditions.

Using contract checking you'll be forcing clients to have .Net Framwork 4.0 or above - as far as performance is concerned, whilst I can't imagine this being a problem, you're best placed testing it out before making a decision one way or another.

Will A
This second part isn't true. The contracts get written into the assembly, so you don't need to reference any additional libraries at runtime. Of course, if you're using CC, it's likely you're using 4.0 anyway :P
Porges
+4  A: 

In some non-mission-critical applications, you may actually want to let the errors slide in a release build rather than throwing an error dialog in the face of the user. Small bugs that the end-user may never notice can cause your contract check to fail and harm user experience.

Mehrdad Afshari
A: 

In many situations, if you want to recover from an exception, you need the details on what caused it - that usually involves creating your own exception classes and proving your details as properties. To use the most basic example, an ArgumentOutOfRangeException contains a property ActualValue, which you might read off and decide what to do based on the value you get.

When using contracts, if your contract fails, you'll just be given a basic ContractException, which is compiler generated, and only contains details about the contract failure in string form, which may need some complex parsing in order to extract what you want from it - you generally wouldn't bother doing that.

If you're using your own exceptions, there should be no reason to keep the contracts in there at runtime, because they'll never be reached if they are about to fail - you'd just be wasting instructions on nothing.

Using static-contract checking alongside your own exception checking is the most effective form of eliminating errors. Most of the time, your exceptions will never be thrown because you're told how to fix the problems before running your app.

Mark H
If I understand correctly there's a generic version of Requires() - Contract.Requires<T>() - that will throw a catchable Exception of type T. Doesn't that address the point you're raising?
urig
How are you going to populate the data for your Exception (ie, call a custom constructor on it) this way? Given my basic example - how are you going to set ActualValue in the ArgumentOutOfRangeException?
Mark H
Provided the Exception of type T has a constructor that takes a single string argument, you can pass this argument to it from the call to Contract.Requires<T>(). This doesn't cover your example of ArgumentOutOfRangeException's ActualValue but it's a start. Maybe future version will have dynamic params to put in the exception?
urig
They could just add it as `Requires<T>(bool condition, T ex)` That would allow you to instantiate your exception in the method, but leave it to the Contract internals to throw it.
Mark H
A: 

In the Code Contracts documentation, there's a section about how to use Code Contracts at runtime. It's better to decide in advance on how you're going to use Code Contracts at runtime, and then proceed following the guidelines in the documentation.

If you're not going to use the Runtime Checker in release builds, you're better of writing legacy 'if...then throw' preconditions instead of Code Contracts preconditions. You can still write postconditions & invariants in your debug build, but they will not be checked at runtime. They are still useful however in your debug build, for debugging purposes with Code Contracts enabled.

KoMet
A: 

I just found this question. I've opened the opposite question. I would like some understanding of the usefulness of runtime Contracts at all. I can't see how they can be of any use, especially in a WCF service. Any guidance or enlightenment would be helpful.

Ken Foster