Implementing Design by Contract in ColdFusion using Coldspring AOP
I have been playing around with implementing Design by Contract in ColdFusion using Coldspring's AOP functionality.
ColdContract is a Coldspring AOP Advice which allows Design by Contract assertions to be added to ColdFusion components and for those assertions to be executed during program execution.
Design by Contract assertions are a mechanism for defining the specification of software components, in such a way as to allow the implementation of the specification to be tested in-line with program execution. This provides a difference (and arguably quicker) method of implementing TDD to Unit Testing.
Assertions are added to components using meta data attributes as shown below:
<!---
/**** Copyright (c) 2008 David Beale (http://www.BealeARTS.co.uk)***/---><cfcomponent displayname="Stack"
hint="A FILO Stack example showing the use of ColdContract assertions. Stack items cannot be objects (Components)."invariants="this.getNumberOfItems() gte 0, this.getNumberOfItems() eq arrayLen(variables.stack)"><!--- INIT ---><cffunction name="init"
hint="Constructor"access="public"returnType="Stack"output="false"><cfreturn this /></cffunction><!--- PUBLIC ---><cffunction name="push"
hint="Push an item onto the stack"access="public"returnType="void"output="false"preconditions="not isObject(arguments.item)"postconditions="this.getNumberOfItems() eq oldThis.getNumberOfItems() + 1"><cfargument name="item" hint="Item to add" type="any" required="true" /><cfset arrayAppend(variables.stack, arguments.item) /><cfset variables.stackIndex++ /></cffunction><cffunction name="pop"
hint="Pop an item off the stack"access="public"returnType="any"output="false"preconditions="this.getNumberOfItems() gt 0"postconditions="this.getNumberOfItems() eq oldThis.getNumberOfItems() - 1, not isObject(cfreturn)"><!--- LOCALS ---><cfset item = '' /><cfset item = variables.stack[variables.stackIndex] /><cfset arrayDeleteAt(variables.stack, variables.stackIndex) /><cfset variables.stackIndex-- /><cfreturn item /></cffunction><cffunction name="getNumberOfItems"
hint="Get the number of items on the stack"access="public"returnType="numeric"output="false"><cfreturn arrayLen(variables.stack) /></cffunction><!--- PRIVATE ---><cfset variables.stack = arrayNew(1) /><cfset variables.stackIndex = 0 /></cfcomponent>- Download this code: stack.cfc

September 1st, 2008 at 12:23 pm
I have the feeling I have seen it somewhere…
http://riait.co.uk/2008/06/25/pre-and-postconditions-for-cfml-functions/
September 1st, 2008 at 2:20 pm
Very cool approach. Thanks for blogging this!
September 3rd, 2008 at 1:02 pm
@radekg
Wow! What can I say, great minds think alike