# Command-Query Separation

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

![wrong way](https://i.imgur.com/f0AChrr.png)

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 `@email` variable. Second bad thing that if email is not valid - it will not set `@email` variable and it will have nil value. That's why email method has to check it every time and return default value if it's nil.

Also, we can't reuse method `validate_email` if we really just want to validate email. Because it sets `@email` variable every time when method is being called.

Let's rewrite that code using CQS:

![right way](https://i.imgur.com/8xtCXKh.png)

This code is much better. Method names are self-explanatory: `set_email`, `valid_email?`. We see that `set_email` it's a __Command__ that will set `@emails` variable. `valid_email?` it's a __Query__ which returns `true` or `false`. Also we can easily re-use `valid_email?` inside class without any unexpected side-effects.

Since `@email` variable will be set only after calling `set_email`, we can easily pull it out into `attr_reader`.

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.


