For example, this article introduces them.
What is the advantage?
Static analysis seems cool, but at the same time, it prevents the possibility of passing null as a parameter to the unit test. (if you followed the example in the article)
While on the topic of unit testing - considering how now, of course, it makes no sense to conclude contracts for code if you already practice automatic testing?
Update
Playing with code contracts, I'm a little disappointed. For example, based on the code in the accepted answer:
public double CalculateTotal(Order order) { Contract.Requires(order != null); Contract.Ensures(Contract.Result<double>() >= 0); return 2.0; }
For unit testing, you still have to write tests to ensure that null cannot be passed, and the result is greater than or equal to zero if the contracts are business logic. In other words, if I were to remove the first contract, the tests would not break if I did not have a test for this function. This is not based on using static analysis built into the best (final, etc.) Visual Studio editions.
In fact, they all boil down to alternative spelling of traditional if statements. My experience of actually using TDD with code contracts shows why and how I talked about it.
Finglas Sep 05 '09 at 15:02 2009-09-05 15:02
source share