Skip to content

Command-Query Separation

Methods should either change state (commands) or return information (queries), but not both, because mixing them makes code harder to reason about and prevents safe re-execution.

Tension Addressed

Fail-Fast vs. Fault Tolerance

Recognizable As

Methods returning void that modify state; methods returning values with no side effects; violations visible as methods that "return and mutate"

Canonical Sources

Meyer, "Object-Oriented Software Construction" (1988)

Transfers To

CQRS Architecture, Functional Programming (pure functions), API Design (safe vs. unsafe operations), Testing (query repeatability)

CQS enables powerful reasoning: queries can be called any number of times without changing system state, commands can be retried safely if idempotent. Mixing obscures both properties.