CQS

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2022 Jan 16 14:37
Editor
Edited
Edited
2026 Mar 9 15:37
Refs
Refs

Command Query Separation

  • Don't return the object created or updated
  • Do query separately

Important Principle of Imperative Programming

Conceived in Bertrand Meyer's Eiffel programming language
Every method should perform only one action: either a command that changes state or a query that returns data
Don't modify the answer when performing an action on a request
In other words, state modification and data return should be implemented separately - this is the theoretical foundation of get and set
Methods should be referentially transparent when returning values (a function is referentially transparent when it doesn't change the system's state) and should not cause side effects
While widely used, the most common problem arising from this is
Race Condition
  • Query - Returns a result value and does not change the observable state of the system. Therefore, it is free of side effects.
  • Command - Does not return a result, but instead changes the state of the system.
Code should be standardized through interfaces, and
CQS
separation is important for extension. What's important at the code level is to prevent it from being too easy to extend. Make the architecture not easy to create a similar function that does the same thing. This enables command query separation, and separating mutation and query files is also a good practice.
 
 
Command와 Query, CQS 알아보기
아는 사람은 알지만, 나는 프로그래밍 교육 기관에서 조교, 코칭 일을 적잖이 했다. 아무래도 요즘 프로그래밍 입문을 파이썬으로 많이 하다보니 파이썬을 공부하는 내가 설 자리가 있었던 것 같다. 그래서 프로그래밍을 처음 하는 사람들에게 변수, 할당, 함수 등 초보적인 개념부터 설명할 일이 많았는데 특히 함수를 공부할 때 list.sort()와 sorted()처럼 비슷한 일을 하는데 구체적인 동작은 다른 형태에 대해 많은 코칭이 필요했다.
Command와 Query, CQS 알아보기
CQS - Command Query Separation
Language/IT devsun 2015. 7. 20. 19:07 Command-query separation (CQS)는 컴퓨터 프로그래밍에서 반드시 지켜야 할 원칙이다. 이것은 Bertrand Meyerd의 Eiffel 프로그래밍 언어에서 고안되었다. 모든 메소드는 한번의 액션에서 상태를 변경하는 커맨드든 데이터를 반환하는 쿼리든 한가지 액션만 취해야 한다는 것이다. 다시 말하자면 질문을 할때 대답을 변경하지 말라는 것이다.
CQS - Command Query Separation
Command-query separation - Wikipedia
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.
Command-query separation - Wikipedia
 
 
 

Recommendations