Everipedia Logo
Everipedia is now IQ.wiki - Join the IQ Brainlist and our Discord for early access to editing on the new platform and to participate in the beta testing.
Command–query separation

Command–query separation

Command–query separation (CQS) is a principle of imperative computer programming. It was devised by Bertrand Meyer as part of his pioneering work on the Eiffel programming language.

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.[1] More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.

Connection with design by contract

Command–query separation is particularly well suited to a design by contract (DbC) methodology, in which the design of a program is expressed as assertions embedded in the source code, describing the state of the program at certain critical times. In DbC, assertions are considered design annotations – not program logic – and as such, their execution should not affect the program state. CQS is beneficial to DbC because any value-returning method (any query) can be called by any assertion without fear of modifying program state.

In theoretical terms, this establishes a measure of sanity, whereby one can reason about a program's state without simultaneously modifying that state. In practical terms, CQS allows all assertion checks to be bypassed in a working system to improve its performance without inadvertently modifying its behaviour. CQS may also prevent the occurrence of certain kinds of heisenbugs.

Broader impact on software engineering

Even beyond the connection with design by contract, CQS is considered by its adherents to have a simplifying effect on a program, making its states (via queries) and state changes (via commands) more comprehensible.

CQS is well-suited to the object-oriented methodology, but can also be applied outside of object-oriented programming. Since the separation of side effects and return values is not inherently object-oriented, CQS can be profitably applied to any programming paradigm that requires reasoning about side effects.

Other architectural patterns

  • As we move away from a single representation that we interact with via CRUD, we can easily move to a task-based UI.

  • CQRS fits well with event-based programming models. It's common to see a CQRS system split into separate services communicating with Event Collaboration. This allows these services to easily take advantage of Event Sourcing.

  • Having separate models raises questions about how hard is to keep those models consistent, which raises the likelihood of using eventual consistency.

  • For many domains, much of the logic required is needed when you're updating, so it may make sense to use Eager Read Derivation to simplify your query-side models.

  • If the write model generates events for all updates, you can structure read models as Event Posters, allowing them to be Memory Images and thus avoiding a lot of database interactions.

  • CQRS is suited to complex domains, the kind that also benefits from Domain-Driven Design. [2]

Command query responsibility segregation

Command query responsibility segregation (CQRS) applies the CQS principle by using separate Query and Command objects to retrieve and modify data, respectively.[3][4]

See also

  • Fluent interface

References

[1]
Citation Linklaser.inf.ethz.chMeyer, Bertrand. "Eiffel: a language for software engineering" (PDF). p. 22. Retrieved 16 December 2014.
Sep 20, 2019, 10:51 AM
[2]
Citation Linkwww.ukad-group.com"What is CQRS". What is CQRS. Retrieved 2019-06-27.
Sep 20, 2019, 10:51 AM
[3]
Citation Linkcqrs.files.wordpress.comYoung, Greg. "CQRS Documents" (PDF). Retrieved 2012-12-28.
Sep 20, 2019, 10:51 AM
[4]
Citation Linkmartinfowler.comFowler, Martin. "CQRS". Retrieved 2011-07-14.
Sep 20, 2019, 10:51 AM
[5]
Citation Linkmartinfowler.comExplanation on Martin Fowler's Bliki
Sep 20, 2019, 10:51 AM
[6]
Citation Linkcqrsjourney.github.comCQRS Journey by Microsoft patterns & practices
Sep 20, 2019, 10:51 AM
[7]
Citation Linkgroups.google.comDDD/CQRS/Event Sourcing List
Sep 20, 2019, 10:51 AM
[8]
Citation Linkwww.cqrs.nuThe CQRS Frequently Asked Questions
Sep 20, 2019, 10:51 AM
[9]
Citation Linkwww.h-online.comCQRS - a new architecture precept based on segregation of commands and queries
Sep 20, 2019, 10:51 AM
[10]
Citation Linklaser.inf.ethz.ch"Eiffel: a language for software engineering"
Sep 20, 2019, 10:51 AM
[11]
Citation Linkwww.ukad-group.com"What is CQRS"
Sep 20, 2019, 10:51 AM
[12]
Citation Linkcqrs.files.wordpress.com"CQRS Documents"
Sep 20, 2019, 10:51 AM
[13]
Citation Linkmartinfowler.com"CQRS"
Sep 20, 2019, 10:51 AM
[14]
Citation Linkmartinfowler.comExplanation on Martin Fowler's Bliki
Sep 20, 2019, 10:51 AM
[15]
Citation Linkcqrsjourney.github.comCQRS Journey by Microsoft patterns & practices
Sep 20, 2019, 10:51 AM
[16]
Citation Linkgroups.google.comDDD/CQRS/Event Sourcing List
Sep 20, 2019, 10:51 AM
[17]
Citation Linkwww.cqrs.nuThe CQRS Frequently Asked Questions
Sep 20, 2019, 10:51 AM
[18]
Citation Linkwww.h-online.comCQRS - a new architecture precept based on segregation of commands and queries
Sep 20, 2019, 10:51 AM
[19]
Citation Linken.wikipedia.orgThe original version of this page is from Wikipedia, you can edit the page right here on Everipedia.Text is available under the Creative Commons Attribution-ShareAlike License.Additional terms may apply.See everipedia.org/everipedia-termsfor further details.Images/media credited individually (click the icon for details).
Sep 20, 2019, 10:51 AM