Design By Contract and Spec#
240 words.
I’ve ragged a bit on The Pragmatic Programmer lately, but I wanted to mention one concept in the book that I really liked: Design By Contract.
Unfortunately, there are no languages that inherently support Design By Contract except arcane languages that nobody has ever heard of. (The Pragmatic Programmer discusses Eiffel and Sather. See what I mean?)
However, a few months back I stumbled upon Spec#, a language variant under research at Microsoft that is specifically built to add Design By Contract concepts to C#.
To describe “Design By Contract,” I could give you some high fallutin’ computer science jargon, but it basically boils down to this: When you declare a method signature, you also declare assert-style validation statements on the incoming parameters and return values. It’s a way to ensure that you have valid data, enforced by the language itself, rather than the programmer.
Without Design By Contract, you might write this:
public int DivideMe( int x, int y )
{
Trace.Assert( y != 0 );
return x / y;
}
With Design By Contract in Spec#, you would instead write:
public int DivideMe( int x, int y )
requires y != 0;
{
return x / y;
}
Pretty cool. Of all the language tricks and gimmicks I see from time to time, this is one of the few that actually appeals to me. It feels like a real improvement, rather than just a change for change’s sake.
Sorry, new comments are disabled on older posts. This helps reduce spam. Active commenting almost always occurs within a day or two of new posts.