The Command-Query Separation (CQS) principle says: functions should do something or answer something, not both.
Commands: change state and don't return anything.
Queries: return a result and don't change state. They're free of side effects.
If you break the CQS... 1/3
Commands: change state and don't return anything.
Queries: return a result and don't change state. They're free of side effects.
If you break the CQS... 1/3
Potential problems:
Unclear whether functions have side effects.
Function names are often deceiving: functions do more than they say.
Violations of Single Responsibility Principle.
Temporal couplings: the order in which you call functions with side effects matters. 2/3
Unclear whether functions have side effects.
Function names are often deceiving: functions do more than they say.
Violations of Single Responsibility Principle.
Temporal couplings: the order in which you call functions with side effects matters. 2/3
Coding tip: follow CQS whenever possible, but don't be too dogmatic. In some cases, it does not fit well.
Example: popping the top element of a stack. The "pop" function removes one element and returns it. Therefore, it modifies the state of the stack and returns a value. 3/3
Example: popping the top element of a stack. The "pop" function removes one element and returns it. Therefore, it modifies the state of the stack and returns a value. 3/3