Command-Query Separation "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."
a = [1, 2, 3]
last = a.pop
Here, in Ruby, the pop command returns the item is popped off the Array.
This an example of a command and query within one method, and it seems necessary for it to be so.
If this is the case, is it really a code smell to have a method that is essentially a query as well as a command?
Stack popping is a well-known exception to CQS. Martin Fowler notes it as a good place to break the rule.
I would say in this case it's not a code smell, but in general it is a code smell.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With