Command-query separation principle

Learn the importance of the command-query separation principle.

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 second time might alter the state of the object in an undesired way.

Get hands-on with 1000+ tech skills courses.

Learn to code, grow your skills, and succeed in your tech interview