Posts

Showing posts from August, 2012

The Command Query Separation Principle

The other day I was making some additions to some legacy code that produces website order extracts which are sent to a third party system. I'd created an order in the test database and then wanted to step through a particular method that collects the data and creates an XML document from it. As I began to step through the code, I had to pause and wonder if this seemingly innocuous method would, over the course of retrieving the data and creating the extract file, also perhaps update the data. Which leads me to this post. The Command Query Separation principle (CQS for short) is a very simple one, but it's also very useful. In short, it states that any method that updates data or modifies state (a command) should not return a result, and any method that returns a result should not update data or modify state. This principle was introduced by Bertrand Meyer in his book "Object Oriented Software Construction". I find this to be a useful and worthwhile principal to fo