BooDesign By Contract in 20 lines of code

time to read 2 min | 225 words

Now, before Greg hurls a modopt on me, I want to be clear that this isn't the same thing that Spec# is doing. But it is a very cool way to specify constraints that must always be valid when a method exists.

Here is the code:

class EnsureAttribute(AbstractAstAttribute):
	expr as Expression
	def constructor(expr as Expression):
		self.expr = expr
	def Apply(target as Node):
		type as ClassDefinition = target
		for member in type.Members:
			method = member as Method
			continue if method is null
			block = method.Body
			method.Body = [|
						assert $expr

And the usage:

[ensure(name is not null)]
class Customer:
	name as string
	def constructor(name as string): = name
	def SetName(newName as string):
		name = newName

Now, any attempt to set the name to null will cause an assertion exception. This technique is quite powerful, and very easy to use. A few years ago I wrote a design by contract implementation for boo that was far more ambitious (handling inheritance, etc). I remember it being much more complicated, and while things like quasi quotation do make it easier, it is not that big a change.

I think that mostly it is the way I write code now, striving to simplicity is something that I am trying to apply recently, and I think it works.

More posts in "Boo" series:

  1. (08 Jan 2018) Serious Cryptography
  2. (25 Nov 2004) Open Source .NET Development