Today i want to talk about a very simple concept "command query separation" which was coined by Bertrand Meyer. Understanding this theoretical concept will heelp you write cleaner and more readable code.
CQS as defined on wikipedia goes like this
It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, Asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.
Seems very simple and straightforward but people in general tend to forget this, i confess, i have.
CURD as an example
One of the most easiest way to understand this would be CURD. CURD has 3 commands i.e Create, Update, Delete which basically change the system and 1 query Read which has no side effects. Simple, right? let's take one more example to see the benifits.
Code as an example
In this example we will see how CQS helps us in picking proper names for method as well as keeps it more readable and maintainable.
Let's take a
User class that has 2 methods
It's a bad code. Let's try to understand why. First of all name of method
validate_email doesn't correspond to its behavior. Besides validation this code has unexpected side-effect: it sets
Also, we can't reuse method
validate_email if we really just want to validate email. Because it sets
Let's rewrite that code using CQS:
This code is much better. Method names are self-explanatory:
valid_email?. We see that
set_email it's a Command that will set
valid_email? it's a Query which returns
false. Also we can easily re-use
valid_email? inside class without any unexpected side-effects.
set_email, we can easily pull it out into
Even on such simple example we see profit of using Command-Query Separation (CQS). In real project this approach will allow you to improve your code and make it more readable. Also one of the biggest benefits of CQS is side-effect free methods - Queries.