Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceptions to the "Command-Query Separation" Rule?

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?

like image 959
RyanScottLewis Avatar asked Feb 25 '26 00:02

RyanScottLewis


1 Answers

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.

like image 143
Josh Kodroff Avatar answered Feb 26 '26 22:02

Josh Kodroff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!