Microsoft Code Contracts
Alvin Ashcraft’s Morning Dew blog entry for today has a really interesting article by DevJourney on Code Contracts.
Since discovering Unit Tests and other checking code, such as Debug.Assert, I’m convinced this is the path to enlightenment in software development. These tools hold up a strong light in what can be a dark mad world of bug-hunting.
Microsoft Code Contracts is a new release from Microsoft. It’s come from the Reseach DevLabs. Essentially, it allows you to define, for a given function, some very specific rules about the variables accepted, and the returns given for the function. These specific rules (the contracts) are then picked up by the IDE, which warns you if your code isn’t definitely meeting its obligations.
This isn’t bug-free code in the theory-based, provable, air-traffic-control sense, but it’s a big leap forward on two fronts. Firstly, it helps you catch those nasty edge cases that end up catching you out. Secondly, you’re enhancing your code with meta-data about how it’s supposed to work and parameters within which you expected the code to run when you wrote it.
The really great news is that Code Contracts is included in the .NET 4 Framework, so you don’t need to add anything to start taking advantage of it.
I’ll leave Developer Journey to provide you with a much better write up than I can offer. Microsoft Research also has an introduction to Code Contracts.