Let's talk about CQS (command query separation).

Command query separation is a term first formulated by Bertrand Meyer who is the creator of "eiffel" programming language. Command query separation essentially states that you should separate your queries from your commands. Is is kind of intuitive name right? But what does that actually mean?

Let's first get it clear that what we are talking about is methods. In other words methods should not mix command operations with query operations, but command query separation is not a concept that is specific to object-oriented programming. What does it actually mean is that a query is when a method is asking some kind of question and a command is when a method is performing some kind of action. So a query is when a method is asking something and you just retrieve a value back from the method. In other words the method returns something. A command on the other hand is when a method performs some kind of action. Let's be less ambiguous, you may already have realized that this is about mutation. So queries do not mutate the state of your application, whereas commands do mutate the state of your application. So in other words this is about separating the methods that mutate the state of your application from the methods that don't do so.

What is the point of separating commands from queries? There are many reasons. The most trivial reason is following. If you've separated commands from queries then you will trivially know when you're calling a method without digging into the method, whether that particular method will be mutating state or not. So you will know which methods that you can call safely because they won't change the state of the application.

So to make your code easier to reason about be sure to separate command methods from query methods or in other words make sure that your methods are not mixing the two things. In other words make sure that you don't have methods that are both performing command operations and returning results. It sounds like a fairly trivial thing to do but then there are some edge cases. Martin Fowler the author of "refactoring" has famously said on his website that he's willing to break the principle in order to get the pop method. You pop on a stack because if you pop on a stack then you are performing a query because you are getting the element but you're also mutating States because you are changing the contents of the snack. In other words you are then performing a command. So there are all these edge cases and these edge cases I think begged for another discussion.