> ## Content Index
> Fetch the complete content index at: https://nolongerset.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Command Query Separation: A Technique for Reducing Code Complexity
- URL: https://nolongerset.com/command-query-separation/
- Published: 2023-09-13T03:54:45.000Z
- Updated: 2026-05-08T12:35:33.000Z
- Description: One helpful way to reduce the complexity of your code is to be intentional about how and where your code produces side effects.
- Author: Mike Wolfe
- Tags: Code That Fits in Your Head, #Import 2026-05-20 02:59

**Useful code** must do at least one of two things:

- Produce a side effect
- Return data

**Simple code** should do one or the other.

---

## What is Command Query Separation?

In his book, *[Code That Fits In Your Head](https://nolongerset.com/code-that-fits-in-your-head/)*, Mark Seemann offers the following advice:

> Don't return data from methods with side effects, and don't cause side effects from methods that return data. If you follow that rule, you can distinguish between these two types of \[procedures\] without having to read the implementation code.  
>  
> This is known as **Command Query Separation (CQS)**.

### What are "Commands"?

A "command" is a procedure that produces a side effect but does not return a value. While there are [exceptions](https://nolongerset.com/subs-vs-functions/#the-caveats-exceptions-and-whatnot), VBA `Sub` routines generally fall into this category.

### What are "Queries"?

In this context, a "query" is a function that returns data but does not produce a side effect. Importantly, this is a separate concept from that of a database query.

### What are Pure Functions?

A query can be either deterministic or non-deterministic. Deterministic queries are known as [pure functions](https://nolongerset.com/what-are-pure-functions/) and have several desirable qualities:

- They are easy to test.
- They readily compose. As Seemann puts it, "If the output of one function fits as the input for another, you can sequentially compose them. Always."
- "You can replace a pure function call with its result," Seemann continues. "The function call is *equal* to the output. The only difference between the result and the function call is the time it takes to get it."

"\[Pure functions\] collapse arbitrary complexity to a single result; a single chunk that fits in your brain."

---

### Related reading

[Code that Fits in Your Head“Code that Fits in Your Head” is to senior software developers what “Code Complete” is to junior software developers. I can think of no higher praise.![](https://nolongerset.com/favicon.png)No Longer SetMike Wolfe![](https://storage.ghost.io/c/eb/d7/ebd732c1-5f03-4f07-b386-5d08557e15c9/content/images/2023/08/Code-That-Fits-in-Your-Head.jpg)](https://nolongerset.com/code-that-fits-in-your-head/)

*Cover image created with [Microsoft Designer](https://designer.microsoft.com/)*