Thoughts about OCL - The Object Constraint Language

Right now I'm taking the Software Architecture and Design class (CS6310) for my graduate degree. Specifically for the last couple of weeks, we have been looking into OCL (Object Constraint Language).

Although all the time I spent studying UML back while in college, and even during all these years that I have used UML, OCL has been completely new to me. For some reason I never heard about it before (or maybe I did, but didn't pay attention.)

The thing is that I've found it fascinating. OCL is a formal language to describe expressions on UML models. You can think of it as a tool to write invariants, pre, and post-conditions for objects and operations in your model.

The first thought I have about OCL is that I already had the problem it solves covered. Anytime I've wanted to describe constraints, I've simply used regular language (either Spanish or English, depending on the audience.) Something like "If this object is blue, then that other object has to be red" has been usually good enough. OCL formalizes this, which is cool, but then forces everyone to understand it.

This leads to my second thought: OCL is something new that you need to learn. Not only the person who is creating the design, but also the people who are going to be reading it. I think this is a huge barrier for OCL. It doesn't matter that I can write cool preconditions in OCL if nobody is able to read them.

The third thing is that existing programing languages sort of do the same as OCL. Why not to specify constraints using pseudocode or near-compiling-code in the language of your choice? How would that be different? Having looked at OCL's syntax, I can see how it's compact and efficient for its task, but I still feel it will be way better to write some quick Java than to learn a new language from scratch.

Finally, somebody on my team noticed that writing OCL feels a whole lot like writing unit tests. That's cool and powerful, but it also makes OCL feel unnecessary and overlapping with already established tools.

There was a poll in my class asking how many people have used OCL in their regular jobs. So far, 41 people have answered no. Nobody has said yes yet.

OCL is not new. The fact that it hasn't been too popular might have something to do with my thoughts above. Would I use it after this class? I'm definitively incorporating some of its syntax sugar into my daily work. However, I don't think I will never do OCL formally.

As always, welcome anything new that can help with your work. Even if that means taking bit and pieces from here and there. OCL has definitively a lot to offer, so instead of completely shutting down the door because nobody else cares, take a look and decide for yourself.

Have something to say about this post? Get in touch!

Want to read more? Visit the archive.