Command query separation

Command query separation is a design principle stating that each method should be either a command that performs an action and mutates state, or a query that returns data to the caller, but never both. The principle was coined by Bertrand Meyer as part of design by contract methodology applied to the Eiffel programming language.

When the principle is applied, programmers can reason about code with much more confidence. Query methods do not mutate state, so they can be called freely, in any order, and from any context without surprising side effects. Commands, by contrast, require more care because they change observable state.

A naming convention that implies whether a method is a query or a command makes the distinction visible at the call site.

Command query separation is one expression of the broader instinct toward separation of concerns, and it shapes how interfaces expose behavior to their clients. The uniform access principle is a related principle, addressing attribute versus method access.

It is related to but distinct from Command Query Responsibility Segregation, which separates commands and queries at the architectural level rather than within a single method. But CQRS builds on the same underlying idea.