nContract

Saturday, May 28, 2005

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


Comments
Wes Haggard Saturday, May 28, 2005
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.
<br>
<br>Thanks,
<br>Wes
Bill Sunday, May 29, 2005
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?
Scott Tuesday, May 31, 2005
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.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!