Command-query Separation - Drawbacks

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.

Read more about this topic:  Command-query Separation

Famous quotes containing the word drawbacks:

    France has neither winter nor summer nor morals—apart from these drawbacks it is a fine country.
    Mark Twain [Samuel Langhorne Clemens] (1835–1910)