Command-query separation principle
Explore the command-query separation principle to understand how methods should either execute actions or return values but not both. Learn to identify violations of this principle in Java, such as side effects in query methods, and see how to refactor code to improve clarity, maintainability, and adherence to design principles within unit testing.
We'll cover the following...
The command-query separation principle states that a method should either execute a command (do something that creates a side effect) or answer a query (return some value), but not both.
In some cases, command-query separation creates a potential pain for client code.
If a query method alters the state of the object, it might not be possible to call it twice (to ask the same question again, for whatever reason) and get the same answer. Similarly, calling it a ...