If you are a fan of Design By Contract, then you’ll find Wes Haggard’s thesis work, nContract, to be an impressive package. nContract allows you to specify contract validations by decorating types and their members with custom attributes. For example:
[Pre("value != null")]
[Post("Contents.Count == old.Contents.Count + value.Length")]
public virtual void Append(string value)
{
stringBuilder.Append(value);
numberOfChars += value.Length;
}
Custom attributes are the perfect place for contract specifications. The pre-condition and post-condition for the Append method will be forever bound to the method and packaged into the metadata. The Append method itself remains clean and easily readable.
To actually perform the contract checking, nContract code-gens a subclass for the component using CodeSmith ( CodeSmith appears to be moving from freeware to commercial, so this might be a bit of a spoiler).
Another core feature in nContract is the ability for clients to configure contract checking in a granular way using configuration files (no recompilations needed), and the ability to hook in a custom assertion handler.
I’ve always been a fan of design by contract, and a bit envious of the Eiffel crowd who get design by contract built-in….