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. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects. It is noteworthy that rigid implementation of this specification makes tracking the number of times queries have been issued essentially impossible; it is clearly intended as a programming guideline rather than a rule for good coding, such as avoiding the use of a goto from a nested loop.

Contents

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 in a manner reminiscent of how Edsger Dijkstra's admonition against gotos did the same for control flow.

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

Drawbacks

CQS can make it more difficult to implement re-entrant and multi-threaded software correctly. This claim usually arises when a non-thread-safe pattern is used to implement the command query separation.

A simple example of a pattern that breaks CQS but is useful for multi-threaded software:

private int x;
public int increment_and_return_x()
{
  lock x;   // by some mechanism
  x = x + 1;
  int x_copy = x;
  unlock x; // by some mechanism
  return x_copy;
}

A common CQS pattern usable only in single threaded applications:

private int x;
public int value()
{
  return x;
}
void increment_x()
{
  x = x + 1;
}

Even in single-threaded programs, it is sometimes arguably significantly more convenient to have a method that is a combined query and command. Martin Fowler cites the pop() method of a stack as an example.[1]

See also

References

  1. ^ http://martinfowler.com/bliki/CommandQuerySeparation.html

Further reading

  • Meyer, Bertrand (1988). Object-oriented Software Construction. Prentice Hall. ISBN 0136290493. 

External links


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Query — In general, a query is a form of questioning, in a line of inquiry. A query may also refer to:* The Queries, a set of 31 questions outlined by Isaac Newton beginning in 1704 * Query (complexity), a mapping from structures of one vocabulary to… …   Wikipedia

  • Eiffel (programming language) — Infobox programming language name = Eiffel paradigm = object oriented year = 1986 designer = Bertrand Meyer developer = Bertrand Meyer Eiffel Software latest release version = 4.2 latest release date = Feb 6, 1998 typing = static typing, strong… …   Wikipedia

  • Domain-driven design — (DDD) is an approach to developing software for complex needs by deeply connecting the implementation to an evolving model of the core business concepts.[1] The premise of domain driven design is the following: Placing the project s primary focus …   Wikipedia

  • CQRS — Эта статья нуждается в дополнительных источниках для улучшения проверяемости. Вы можете помочь улучшить эту статью, добавив ссылки на авторитетные источники. Не подтверждённая источниками информация может …   Википедия

  • Logo (programming language) — LOGO redirects here. For other uses, see LOGO (disambiguation). Logo Paradigm(s) multi paradigm:functional educational, procedural, reflective Appeared in …   Wikipedia

  • Referential transparency (computer science) — Referential transparency and referential opaqueness are properties of parts of computer programs. An expression is said to be referentially transparent if it can be replaced with its value without changing the program (in other words, yielding a… …   Wikipedia

  • Subroutine — In computer science, a subroutine (function, method, procedure, or subprogram) is a portion of code within a larger program, which performs a specific task and can be relatively independent of the remaining code. The syntax of many programming… …   Wikipedia

  • Unusual software bug — Unusual software bugs are a class of software bugs that are considered exceptionally difficult to understand and repair. There are several kinds, mostly named after scientists who discovered counterintuitive things. Contents 1 Bohrbug 2 Mandelbug …   Wikipedia

  • CQS — may refer to:* Command query separation * Consolidated Quotation System …   Wikipedia

  • Rudy Giuliani — Giuliani in 2006 107th Mayor of New York City In office January 1, 1994 – December 31, 2001 Preceded by …   Wikipedia


Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”

We are using cookies for the best presentation of our site. Continuing to use this site, you agree with this.