Code contracts benefits
.net, c#, optimization
Solution
According to the MSDN:
The benefits of code contracts include the following:
- Improved testing: Code contracts provide static contract verification, runtime checking, and documentation generation.
- Automatic testing tools: You can use code contracts to generate more meaningful unit tests by filtering out meaningless test arguments that do not satisfy preconditions.
- Static verification: The static checker can decide whether there are any contract violations without running the program. It checks for implicit contracts, such as null dereferences and array bounds, and explicit contracts.
- Reference documentation: The documentation generator augments existing XML documentation files with contract information. There are also style sheets that can be used with Sandcastle so that the generated documentation pages have contract sections.
Your mileage may vary which of those points are important or not. I found the third and fourth (static verification and documentation) especially interesting.
In other words, it is more structured way of describing contracts (instead of constructs like `if (x!=null){}`) that additional tools are able to understand.
Problem
Why should I use code contracts like ``` Contract.Requires<ArgumentNullException>( x != null, "x" ); ``` instead of good old ``` if (x!=null){} else throw... ``` Are there any other benefits except of conciseness ?