The last mature answer to this was in 2009, but .NET 4 is missing. I suppose we need an update:
Code contracts may well be mature enough for your Debug releases.
I understand that this is more likely an upgrade from "harmless" to "basically harmless."
The Code Contracts home page contains links to fairly comprehensive PDF documents. The documentation provides recommendations for use in section 5. To summarize, you can choose how brave you feel that the contract tools are rewriting your IL in your release versions.
We use the "Do not rewrite my IL release" mode.
So far, I liked this unexpected advantage the most: there is less code, so less code to test . All your protective disclaimers are melting.
if(arg != null) { throw new ArgumentNullException("arg"); } // Blank line here insisted upon by StyleCop
becomes:
Contract.Requires(arg != null);
Your functions are shorter. Your intention is clearer. And you no longer need to write a test called ArgumentShouldNotBeNull to achieve 100% coverage.
So far I have had two problems:
I had a unit test that relied on contract failure. You can argue that having a test was a mistake, but I wanted to write this specific prohibition in the form of a test. The test failed on my build server because I did not have the tools installed. Solution: install tools.
We use two tools that rewrite IL: Code Contracts and PostSharp . They did not get along too well. PostSharp 2.0.8.1283 fixed the problem. I would carefully evaluate how all two IL rewriting tools work.
So far, the benefits have outweighed the dangers.
Resolving obsolete issues raised in other answers:
- The documentation for code contracts is fairly comprehensive, although, unfortunately, in PDF format.
- There is at least one forum with a contract code hosted by Microsoft.
- Contract Code The standard version is free if you have a VS2010 license.
- .NET 4 is missing. I came across Microsoft contracts when implementing common collection interfaces.
Garth Kidd Aug 09 '10 at 2:07 2010-08-09 02:07
source share