nContract

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….

posted on Friday, May 27, 2005 11:48 PM by scott

Comments

Saturday, May 28, 2005 10:58 AM by Wes Haggard

# re: nContract

Thanks for the plug. I was unaware of CodeSmith moving completly away from freeware. If that does happen and people are interested I would be happy to move it to another (free) code generator or develop my own custom template generation for nContract's specific needs.

Thanks,
Wes
Sunday, May 29, 2005 12:55 PM by Bill

# re: nContract

Scott - I just started getting into contracts and so far, I'm digging them. In practice, have you found that they were a good fit?
Monday, May 30, 2005 9:23 PM by Scott

# re: nContract

I love contracts. I'm hoping someday more languages will support the ability to define preconditions, postconditions, and invariants using a declarative syntax like this. It would also be useful to declare constraints on value types.