Readable Code and Fast Queries Share the Same Secret: Make the Machine's Job Obvious
Hatched by Kai Nguyen
May 23, 2026
9 min read
2 views
86%
The hidden question behind good code and fast SQL
What do a crisp docstring and a fast database query have in common?
At first glance, almost nothing. One lives in the world of human communication, the other in the world of execution plans, indexes, and filtering strategies. Yet both are solving the same problem: how to make intent obvious enough that the system can do the right thing without guessing.
That is the deeper tension here. Good code is not just code that works. Good SQL is not just SQL that returns the right rows. In both cases, the real challenge is to reduce ambiguity. A reader should immediately understand what a function is for. A database engine should immediately see how to retrieve the rows with the least possible work.
The best instructions are not the cleverest ones. They are the ones that leave the fewest decisions to the machine.
This is why an elegant docstring and a sargable query feel strangely similar. Both are acts of discipline. Both resist the temptation to hide meaning inside indirection, cleverness, or excess. And both reward you for front-loading the most important information in the format the system can use most efficiently.
Why clarity is a performance feature
In programming, we often treat readability as a courtesy to other humans, something nice to have after the “real” work is done. In databases, we often treat performance as a technical concern, something separate from language and style. That split is misleading.
Clarity is a performance feature because systems, whether human or computational, are expensive when they have to infer intent.
Consider a docstring. A one-line summary is enough for an obvious case, but only when the function is truly obvious. If the behavior is more nuanced, the convention is to begin with a summary line, then a blank line, then a fuller explanation. That structure matters. It gives the reader a fast answer first, then details on demand. It respects cognitive bandwidth.
SQL query structure works the same way. If you filter early with a WHERE clause, use appropriate indexes, and avoid unnecessary calculations, you are doing the database equivalent of writing a clear summary line. You are saying: start here, not everywhere. Use this path, not every path. Don’t do more work than needed.
A slow query is often a query that makes the engine read too much, sort too much, or compute too much before it knows what matters. A bad docstring does something psychologically similar: it forces the human reader to scan too much, interpret too much, and reconstruct intent from scattered hints. The cost is not identical, but the principle is the same.
The anti-pattern: making the system guess
Both domains punish a common mistake: forcing the system to reverse engineer your intention.
In SQL, that shows up when you apply arithmetic to indexed columns in the WHERE clause, wrap columns in functions, negate conditions in ways that block index use, or begin pattern matches with a wildcard. These choices may be syntactically valid, but they blur the structure that could have helped the optimizer find a cheap route. The engine can still answer the question, but it now has to work harder to discover how.
In documentation, the same anti-pattern appears when the main point is buried under prose, when a function gets a vague paragraph instead of a precise summary, or when the obvious thing is explained in a way that makes it feel less obvious. A docstring that rambles before stating the purpose is like a query that sorts before filtering. It puts effort in the wrong place.
Here is the shared lesson: the cheapest operation is the one you make possible before the system has to infer anything.
That is why good docstrings open with the summary line. That is why good queries filter early. That is why the most useful structures are often the least glamorous ones. They create a shape that both people and machines can follow without friction.
Think of it this way: if the system cannot see your intent, it has to simulate a search process. The more it has to search, the more expensive your design becomes.
Sargability as a philosophy of expression
The word sargable is usually introduced as a database optimization concept, but it is more revealing than that. It means the query is written in a way that can use an index efficiently. In other words, the query is not merely correct, it is searchable in the right shape.
That idea can be generalized into a philosophy of expression.
A well written docstring is sargable for humans. It is easy to search mentally, easy to scan, and easy to match against the question the reader is asking. The first sentence acts like an index entry. The blank line separates the summary from the detail, allowing the reader to stop early if they already have what they need. The fuller description is there for the cases where nuance matters.
A well written SQL query is sargable for the database. It lets the optimizer use an index, limit the result set, and avoid unnecessary sorting and grouping. The query is aligned with the structure of the data rather than fighting it.
This yields a useful mental model:
- State the primary key of meaning first. In a docstring, that is the purpose. In SQL, that is the filter.
- Preserve the native shape of the system. In code, that means using the conventions the reader expects. In SQL, that means writing predicates that the engine can optimize.
- Delay complexity until it is necessary. In documentation, details come after the summary. In querying, expensive operations come after reduction.
Sargability, then, is not just about speed. It is about respecting the form in which a system can understand you most directly.
Good expression does not decorate meaning. It encodes meaning in a form that is cheap to recognize.
The blank line and the index: two small structures with outsized power
The most interesting overlap between these domains is that their biggest wins often come from tiny structural decisions.
In docstrings, a blank line separates the short summary from the long explanation. That tiny gap is not cosmetic. It creates a two-level interface: scan first, read deeply second. It lets a tool extract the summary cleanly, and it lets a human decide whether to keep going. That is a surprisingly powerful pattern for knowledge work in general.
In SQL, an index can feel similarly invisible until the moment it matters. A properly chosen index is not a decoration, it is a precomputed path through the data. It changes the economics of the query. Instead of searching the whole table, the engine can jump to the relevant region and stop earlier. A single design choice can convert a brute force operation into a targeted one.
The commonality is structural leverage. Neither the blank line nor the index says much by itself. But each creates a constraint that makes the system smarter.
This is a deeper lesson about precision: the most powerful improvements are often not more content, but better boundaries.
A docstring boundary tells you where the summary ends and the elaboration begins. An index boundary tells the database where the relevant rows are likely to live. In both cases, boundaries reduce uncertainty.
A practical framework: write for the first pass, then for the second pass
One way to unify these ideas is to think in terms of two passes.
The first pass is about fast recognition. The second pass is about detail.
For documentation, the first pass is the one-line summary. It should answer the question: what does this do, in one breath? If the function is obvious, keep it concise. If it is not, still begin there. The second pass provides nuance, edge cases, and constraints.
For SQL, the first pass is the most selective, index-friendly part of the query. It should answer the question: what can we eliminate immediately? The second pass handles any remaining logic, formatting, or final shaping of the result.
This suggests a practical rule that applies beyond code and databases:
- Front-load the discriminating signal.
- Defer the expensive interpretation.
- Use structure to make the first pass cheap.
- Use detail only after the cheap path has done its job.
Imagine you are asking a librarian for a book. The best request is not a long speech about your research process. It is a short, precise statement of what you need, followed by enough context if the first answer is insufficient. That is the same architecture as a good docstring or a well shaped query.
Or imagine a warehouse. If every item is stored randomly, retrieval means searching the entire building. If similar items are organized and indexed, retrieval becomes a focused trip to the right aisle. Documentation and SQL both improve when meaning is arranged so retrieval is possible.
The deeper synthesis: elegance is compression with no loss of meaning
At the center of both conventions is a paradox: the best expression is often the most compressed one, but only if compression does not destroy meaning.
A one-line docstring is powerful when the function is obvious because it compresses intent without omitting anything important. A full docstring is powerful when the behavior is subtle because it compresses the essential story into a standard structure. Likewise, a sargable query is powerful because it compresses the search space without changing the result set.
This is what makes the connection between these topics genuinely interesting. They are both about semantic compression.
Semantic compression means representing an idea in a form that preserves meaning while minimizing the work required to recover it. For humans, that means a clear summary and a predictable structure. For databases, that means predicates and expressions that map cleanly onto indexes and execution plans.
When compression is done well, the system expends energy only on what matters. When it is done poorly, the system wastes energy reconstructing what should have been made explicit.
That is why “always use triple double quotes” matters less as a technical preference than as a convention of readability. It standardizes the container. Similarly, why “use appropriate indexes” matters less as a random performance tip than as a commitment to preserve meaningful structure. It standardizes the path to retrieval.
The point is not that code and SQL are the same. The point is that both reward a kind of design that makes truth easier to find.
Key Takeaways
- Lead with the most searchable truth. In a docstring, that is the purpose. In SQL, that is the selective filter.
- Avoid forcing inference. Anything that makes the reader or the optimizer guess, whether vague prose or function wrapped predicates, adds cost.
- Use structure as a signal. Blank lines, standard quotation style, indexes, and early filtering all reduce ambiguity.
- Separate overview from detail. Give the fast answer first, then the explanation that handles edge cases.
- Treat clarity as a form of optimization. The clearer the expression, the less work required to understand or execute it.
Closing thought: the best systems are easy to read and easy to run
We usually talk about readability and performance as if they belong to different worlds. One is for humans, the other for machines. But that division hides a more important truth: the best systems make the human intention and the machine execution line up as closely as possible.
A good docstring tells the reader what matters without making them dig. A good query tells the optimizer what matters without making it search blindly. In both cases, the art is not in adding cleverness. It is in removing friction.
So the next time you write a summary line or a WHERE clause, ask a better question than “Is this correct?” Ask: Can the next reader, human or machine, see the shape of the answer immediately?
That is where elegance begins.
Sources
Hatch New Ideas with Glasp AI 🐣
Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)
Start Hatching 🐣