Skip to content

Commit aef30ad

Browse files
iluwatarwebpro
authored andcommitted
Add command query separation principle
1 parent 93dbee1 commit aef30ad

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ The list was inspired by [The Principles of Good Programming](http://www.artima.
3434
* [Curly's Law](#curlys-law)
3535
* [Encapsulate What Changes](#encapsulate-what-changes)
3636
* [Interface Segregation Principle](#interface-segregation-principle)
37+
* [Command Query Separation](#command-query-separation)
3738

3839
## KISS
3940

@@ -392,3 +393,23 @@ How
392393
Resources
393394

394395
* [Opportunistic Refactoring](http://martinfowler.com/bliki/OpportunisticRefactoring.html)
396+
397+
## Command Query Separation
398+
399+
The Command Query Separation principle states that each method should be either a command that performs an action or a query that returns data to the caller but not both. Asking a question should not modify the answer.
400+
401+
With this principle applied the programmer can code with much more confidence. The query methods can be used anywhere and in any order since they do not mutate the state. With commands one has to be more careful.
402+
403+
Why
404+
405+
* By clearly separating methods into queries and commands the programmer can code with additional confidence without knowing each method's implementation details.
406+
407+
How
408+
409+
* Implement each method as either a query or a command
410+
* Apply naming convention to method names that implies whether the method is a query or a command
411+
412+
Resources
413+
414+
* [Command Query Separation in Wikipedia](https://en.wikipedia.org/wiki/Command%E2%80%93query_separation)
415+
* [Command Query Separation by Martin Fowler](http://martinfowler.com/bliki/CommandQuerySeparation.html)

index.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ The list was inspired by [The Principles of Good Programming](http://www.artima.
4141
* [Curly's Law](#curly-39-s-law)
4242
* [Encapsulate What Changes](#encapsulate-what-changes)
4343
* [Interface Segregation Principle](#interface-segregation-principle)
44+
* [Command Query Separation](#command-query-separation)
4445

4546
## KISS
4647

@@ -399,3 +400,23 @@ How
399400
Resources
400401

401402
* [Opportunistic Refactoring](http://martinfowler.com/bliki/OpportunisticRefactoring.html)
403+
404+
## Command Query Separation
405+
406+
The Command Query Separation principle states that each method should be either a command that performs an action or a query that returns data to the caller but not both. Asking a question should not modify the answer.
407+
408+
With this principle applied the programmer can code with much more confidence. The query methods can be used anywhere and in any order since they do not mutate the state. With commands one has to be more careful.
409+
410+
Why
411+
412+
* By clearly separating methods into queries and commands the programmer can code with additional confidence without knowing each method's implementation details.
413+
414+
How
415+
416+
* Implement each method as either a query or a command
417+
* Apply naming convention to method names that implies whether the method is a query or a command
418+
419+
Resources
420+
421+
* [Command Query Separation in Wikipedia](https://en.wikipedia.org/wiki/Command%E2%80%93query_separation)
422+
* [Command Query Separation by Martin Fowler](http://martinfowler.com/bliki/CommandQuerySeparation.html)

0 commit comments

Comments
 (0)