<s>
Command-query	B-Application
separation	I-Application
(	O
CQS	O
)	O
is	O
a	O
principle	O
of	O
imperative	B-Application
computer	B-General_Concept
programming	I-General_Concept
.	O
</s>
<s>
It	O
was	O
devised	O
by	O
Bertrand	O
Meyer	O
as	O
part	O
of	O
his	O
pioneering	O
work	O
on	O
the	O
Eiffel	B-Language
programming	I-Language
language	I-Language
.	O
</s>
<s>
It	O
states	O
that	O
every	O
method	B-Language
should	O
either	O
be	O
a	O
command	O
that	O
performs	O
an	O
action	O
,	O
or	O
a	O
query	O
that	O
returns	O
data	O
to	O
the	O
caller	O
,	O
but	O
not	O
both	O
.	O
</s>
<s>
Command-query	B-Application
separation	I-Application
is	O
particularly	O
well	O
suited	O
to	O
a	O
design	B-Application
by	I-Application
contract	I-Application
(	O
DbC	B-Application
)	O
methodology	O
,	O
in	O
which	O
the	O
design	O
of	O
a	O
program	B-Application
is	O
expressed	O
as	O
assertions	O
embedded	O
in	O
the	O
source	O
code	O
,	O
describing	O
the	O
state	B-Application
of	O
the	O
program	B-Application
at	O
certain	O
critical	O
times	O
.	O
</s>
<s>
In	O
DbC	B-Application
,	O
assertions	O
are	O
considered	O
design	O
annotations	O
—	O
not	O
program	B-Application
logic	O
—	O
and	O
as	O
such	O
,	O
their	O
execution	O
should	O
not	O
affect	O
the	O
program	B-Application
state	B-Application
.	O
</s>
<s>
CQS	O
is	O
beneficial	O
to	O
DbC	B-Application
because	O
any	O
value-returning	O
method	B-Language
(	O
any	O
query	O
)	O
can	O
be	O
called	O
by	O
any	O
assertion	O
without	O
fear	O
of	O
modifying	O
program	B-Application
state	B-Application
.	O
</s>
<s>
In	O
theoretical	O
terms	O
,	O
this	O
establishes	O
a	O
measure	O
of	O
sanity	O
,	O
whereby	O
one	O
can	O
reason	O
about	O
a	O
program	B-Application
's	O
state	B-Application
without	O
simultaneously	O
modifying	O
that	O
state	B-Application
.	O
</s>
<s>
Even	O
beyond	O
the	O
connection	O
with	O
design	B-Application
by	I-Application
contract	I-Application
,	O
CQS	O
is	O
considered	O
by	O
its	O
adherents	O
to	O
have	O
a	O
simplifying	O
effect	O
on	O
a	O
program	B-Application
,	O
making	O
its	O
states	O
(	O
via	O
queries	O
)	O
and	O
state	B-Application
changes	O
(	O
via	O
commands	O
)	O
more	O
comprehensible	O
.	O
</s>
<s>
CQS	O
is	O
well-suited	O
to	O
the	O
object-oriented	B-Language
methodology	O
,	O
but	O
can	O
also	O
be	O
applied	O
outside	O
of	O
object-oriented	B-Language
programming	I-Language
.	O
</s>
<s>
Since	O
the	O
separation	O
of	O
side	O
effects	O
and	O
return	O
values	O
is	O
not	O
inherently	O
object-oriented	B-Language
,	O
CQS	O
can	O
be	O
profitably	O
applied	O
to	O
any	O
programming	O
paradigm	O
that	O
requires	O
reasoning	O
about	O
side	O
effects	O
.	O
</s>
<s>
Command	O
query	O
responsibility	O
segregation	O
(	O
CQRS	O
)	O
generalises	O
CQS	O
to	O
message-driven	O
and	O
event-driven	B-Application
architectures	I-Application
:	O
it	O
applies	O
the	O
CQS	O
principle	O
by	O
using	O
separate	O
Query	O
and	O
Command	O
messages	O
to	O
retrieve	O
and	O
modify	O
data	O
,	O
respectively	O
.	O
</s>
<s>
As	O
we	O
move	O
away	O
from	O
a	O
single	O
representation	O
that	O
we	O
interact	O
with	O
via	O
CRUD	B-General_Concept
,	O
we	O
can	O
easily	O
move	O
to	O
a	O
task-based	O
UI	O
.	O
</s>
<s>
This	O
allows	O
these	O
services	O
to	O
easily	O
take	O
advantage	O
of	O
Event	B-Application
Driven	I-Application
Architecture	I-Application
.	O
</s>
<s>
CQS	O
can	O
introduce	O
complexities	O
for	O
implementing	O
reentrant	B-Operating_System
and	O
multithreaded	B-Operating_System
software	O
correctly	O
.	O
</s>
<s>
This	O
usually	O
occurs	O
when	O
a	O
non-thread-safe	O
pattern	O
is	O
used	O
to	O
implement	O
the	O
command-query	B-Application
separation	I-Application
.	O
</s>
<s>
Here	O
is	O
a	O
simple	O
example	O
that	O
does	O
not	O
follow	O
CQS	O
,	O
but	O
is	O
useful	O
for	O
multi-threaded	B-Operating_System
software	O
because	O
it	O
solves	O
the	O
complexity	O
of	O
locking	O
for	O
all	O
other	O
parts	O
of	O
the	O
program	B-Application
,	O
but	O
by	O
doing	O
so	O
it	O
does	O
n't	O
follow	O
CQS	O
because	O
the	O
function	O
both	O
mutates	O
state	B-Application
and	O
returns	O
it	O
:	O
</s>
<s>
Note	O
that	O
it	O
is	O
safely	O
usable	O
only	O
in	O
single-threaded	B-Operating_System
applications	O
.	O
</s>
<s>
In	O
a	O
multithreaded	B-Operating_System
program	B-Application
,	O
there	O
is	O
a	O
race	O
condition	O
in	O
the	O
caller	O
,	O
between	O
where	O
increment( )	O
and	O
value( )	O
would	O
be	O
called	O
:	O
</s>
<s>
Even	O
in	O
single-threaded	B-Operating_System
programs	O
,	O
it	O
is	O
sometimes	O
arguably	O
significantly	O
more	O
convenient	O
to	O
have	O
a	O
method	B-Language
that	O
is	O
a	O
combined	O
query	O
and	O
command	O
.	O
</s>
<s>
Martin	O
Fowler	O
cites	O
the	O
pop( )	O
method	B-Language
of	O
a	O
stack	B-Application
as	O
an	O
example	O
.	O
</s>
