Object Constraint Language
Geez, I don’t know why I haven’t bumped into this before, but while doing some research on Test Driven Development for my current client, I ran across the Object Constraint Language. [corrected his name from Josh to Jason. doh!] Jason Gorman has released a cool slide deck on OCL for .Net developers that you should check out (plus check out the other slides on UML for .Net developers). You use it to supplement your UML diagrams. Instead of having free form text in the UML constraints, you use OCL instead. The thing that I like about it is that it is totally declarative. It is an expression that can only evaluate to true or false. OCL is also a C like language, so it is easy to convert to C#. Josh talks about the different ways we implement constraints in our code. There is the Debug.Assert() method, which most C++ programmers use (Design By Contract), and the Defensive Programming version, throwing Exceptions. Since Debug.Assert isn’t a viable way to implement constraints in .Net (it throws a PopUp Box and is meant for debugging only), most .Net developers use the Defensive Programming style. That is, before you do any real work in a method, you check the pre-conditions and throw exceptions if there is a problem. This is the style that I’ve been using in most of my apps.
But now seeing OCL, and with Josh’s points about Design By Contract versus Defensive Programming, it hit me that there must be a good way to implement the best of the 3 models into .Net and come up with a really cool implementation. I mean, constraints are mostly business rules (along with enforcing contracts), and I’d like a way to pull them out of the code and into the documentation (sort of like the C# XML comments), plus have a standard way of implementing them. Plus, if you could bind constraints to the exception that it can throw, now that would be cool documentation. What would the implementation look like? I’ve got some ideas, but nothing that I’m happy with, so that’s why I’m blogging this. I’m counting on feedback from the readers, and maybe we can put our heads together and come up with something. I was originally thinking all the lines of using attributes, sort of like what we do with NUnit, but I haven’t figured out the details.
DonXML Demsak
The preceding blog entry has been syndicated from the DonXML Demsak’s All Things Techie Blog. Please post all comments on the original post, and not this one.