Skip to content

Commit 79664bb

Browse files
author
Greg Bowler
committed
document query-command separation, closes #18
1 parent 34daa06 commit 79664bb

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Please see a simplified bulleted list below. Click the headings for more informa
121121
+ Limit function length
122122
+ Ensure return points are consistent
123123
+ A function should only ever do one thing, as indicated by its name
124+
+ Functions should either mutate or query the system, never both
124125
+ Use camelCase for variable naming
125126
+ Never use global variables
126127
+ Properties should always be declared

general/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
+ Limit function length
2727
+ Ensure return points are consistent
2828
+ A function should only ever do one thing, as indicated by its name
29+
+ Functions should either mutate or query the system, never both
2930

3031
## [Variables](variables.md)
3132

general/functions.md

+10
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,13 @@ Always be as specific as possible with the name of a function, so it describes e
9494
Another rule of thumb is to be wary of words like "and" and "or" in function names, as these indicate that the function does more than one thing.
9595

9696
A benefit of this extra verbosity is that your code becomes extra searchable by IDEs. In the above examples, the multiple functions allow developers to search for functions containing the word "vegetarian", allowing for much faster navigation through the code.
97+
98+
## Functions should either mutate or query the system, never both
99+
100+
For clarity throughout your code, a function's implementation should take the form of a mutator or query. This is sometimes referred to as "[command/query separation][command-query-separation]".
101+
102+
A "mutator", or "command" performs an action, changing the state of the system. For example, `$userRepo->createNewUser("cody")`, `$shop->emptyCart()`, `$user->setVisibility(true)`, `$ui->showNotification("Item added to cart")`.
103+
104+
A "query" retrieves information without having any side effects. For example, `$userRepo->getUserById(105)`, `$item->containsMeat()`, `$input->validate()`.
105+
106+
[command-query-separation]: https://www.martinfowler.com/bliki/CommandQuerySeparation.html

0 commit comments

Comments
 (0)