CQS


Last Updated on Sep 09, 2021

Command–query separation (CQS) is a principle of imperative programming, which states that every method should either be a Command that performs an action or a Query that returns data, but not both.

Simply put, asking a question should not change the answer.

CQS can have a simplifying effect on a program, making its states (via queries) and state changes (via commands) more understandable and easier to reason about. It establishes a measure of sanity, whereby one can reason about a program's state without simultaneously modifying that state.

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


Related:

© 2022 Ambitious Systems. All Rights Reserved.