Readable Code and Fast Queries Obey the Same Law: Make the Intent Visible
Hatched by Kai Nguyen
Jun 05, 2026
9 min read
5 views
84%
The hidden cost of hiding intent
What do a Python docstring and a SQL index have in common? At first glance, almost nothing. One helps a human understand code, the other helps a database answer a question faster. But both are really about the same discipline: making intent legible to the next reader, whether that reader is a person or a machine.
That is the deeper tension here. We often treat clarity and performance as separate virtues, even competing ones. We write docstrings for humans and tune SQL for engines. Yet the strongest systems, whether codebases or query plans, are built by respecting a single principle: the thing that should be obvious should be made obvious, and the thing that should be searchable should be made searchable.
When we fail at this, we pay twice. Humans must infer what a function or query is doing. Machines must do extra work because the structure obscures the pattern they need to exploit. The result is the same in both worlds: ambiguity becomes expense.
The best code and the best queries do not merely work. They declare their shape in advance.
That is why docstring conventions and SQL execution order belong in the same conversation. They are both manuals for preserving intent under pressure: pressure from time, from complexity, from future edits, from scale.
Clarity is not decoration, it is an optimization strategy
A common mistake is to think of documentation as commentary added after the real work is done. Likewise, many people think of query optimization as a bag of tricks for emergencies. But both are better understood as design constraints. They shape how information is expressed so that it can be consumed efficiently.
A one line docstring is enough for really obvious cases. That rule sounds modest, but it reveals something important: not every idea deserves the same amount of explanation. If the purpose is obvious, say so plainly and move on. If the purpose is subtle, use a summary line, then a blank line, then a fuller explanation. The structure itself teaches the reader how to read the thing.
SQL rewards the same restraint. If a query forces the engine to compute on every row before filtering, or to sort more data than needed, or to apply a leading wildcard that destroys index usage, it is asking the database to guess. It hides the shape of the problem. A SARGable predicate does the opposite: it frames the question in a way the engine can answer efficiently.
Consider the difference between these two mental models:
- Poor expression: "I know what I want, so I will write it however feels natural."
- Intentional expression: "I know what I want, so I will phrase it in a form that preserves both meaning and searchability."
That difference matters in code comments, docstrings, SQL, APIs, and even product requirements. Clarity is not a layer added for politeness. It is an optimization strategy for cognition and computation.
The real enemy is not complexity, it is unnecessary transformation
Both topics point toward the same anti pattern: transforming data or meaning more than necessary before the system can act on it.
In Python, a docstring is meant to provide the shortest path from question to answer. The convention of a summary line followed by a blank line and then elaboration creates a hierarchy of access. A reader can stop at the first line if they only need orientation, or continue deeper if they need nuance. That is an information architecture problem, not a stylistic one.
In SQL, the same hierarchy exists in the execution path. The database wants to filter early, reduce the working set, and avoid expensive operations on rows that will later be discarded. If you wrap an indexed column in arithmetic, negation, or a function inside the WHERE clause, you often prevent the optimizer from using the index effectively. You have taken a directly searchable value and buried it under transformation.
This is a subtle but profound idea: transformation is expensive when it obscures the original shape of the problem.
Imagine a librarian who must find all books published after 2020. If the books are sorted by publication year, the search is fast. But if every year is stored as a string that must be converted, reversed, or combined with other metadata before sorting, the librarian can no longer simply scan the shelf. The information is still there, but the path to it has become indirect.
That is what happens when queries become clever at the expense of legibility. A condition like WHERE YEAR(order_date) = 2024 may feel readable to the human eye, but it often forces the engine to compute the year for every row instead of using a date index cleanly. The query still says what it means, but it no longer says it in a machine friendly way.
The same warning applies to docstrings. An overly clever docstring, or one buried in jargon, can force the reader to perform extra mental transformation just to understand a function that should have been obvious. The documentation becomes a puzzle instead of a guide.
SARGability and good docstrings are both promises
At their best, both conventions create a promise about how to approach a problem.
A good docstring promises: you do not need to reverse engineer this to know what it does.
A SARGable query promises: you do not need to brute force this to know how to find it.
That shared promise changes the relationship between creator and consumer. The creator stops optimizing only for immediate convenience and starts optimizing for downstream cost. The consumer, human or machine, can move faster because the path has been made narrower and clearer.
This is why the instruction to always use triple double quotes matters. It is not merely a formatting preference. It standardizes expectation. A predictable form makes automated tooling easier, keeps conventions consistent, and lowers cognitive friction for readers scanning a codebase. Small syntax choices become part of a larger contract of readability.
SQL conventions work similarly. Use appropriate indexes. Limit result set size. Avoid unnecessary sorting and grouping. Filter early. Avoid unnecessary calculations and functions. Each recommendation does not merely improve speed in isolation. Together they preserve the possibility that the engine can recognize the problem’s shape and choose the best path.
There is a powerful mental model here:
Good expression is the art of leaving enough structure intact for the next agent to do its job well.
That next agent may be a teammate reading the code at 2 a.m. It may be the SQL optimizer deciding whether to scan or seek. It may even be your future self, returning to a file six months later and trying to remember why a function exists at all.
A practical framework: explain, expose, preserve
If we combine these ideas into a single framework, we get three obligations for any well formed technical artifact.
1. Explain the purpose
A docstring begins with a summary line because the first job is orientation. State what the function or module is for, not how you happened to implement it.
In SQL, this means writing the query around the business question before reaching for procedural shortcuts. What exactly are you asking for? Which filters matter first? What is the smallest result set that answers the question?
2. Expose the structure
A reader should be able to see the shape of the thing quickly. In documentation, that means a concise summary, blank line, then elaboration if needed. In SQL, that means expressions that keep key columns visible to indexes and the optimizer.
Compare these two approaches:
WHERE created_at >= '2024-01-01'WHERE DATE(created_at) = '2024-01-01'
Both express a time based filter, but the first exposes the structure of the data better. It preserves the searchable form. The second may hide it behind a function call.
Likewise, compare a docstring that says:
Return the user record for the given ID.
versus one that says:
Handles record retrieval and validation across multiple services, with fallback behavior and compliance checks, unless certain state transitions apply.
The second may be accurate, but it is not immediately usable. It forces the reader to excavate meaning. The first tells you what to expect.
3. Preserve what can be optimized later
When you write a query, resist unnecessary negation, nested calculations, and broad result sets that will later be whittled down. Let the engine do work at the right stage, not before.
When you write documentation, resist over explaining obvious things, and resist burying the key point beneath nuance that belongs elsewhere. Let the reader find the answer at the right level, not after digging through prose that tries to prove how much you know.
This framework applies well beyond Python and SQL. It applies to architecture diagrams, API design, logs, incident runbooks, and product specs. The core question is always the same: what should be visible early, and what should remain directly usable instead of transformed beyond recognition?
The deeper lesson: machines and humans reward the same honesty
It is tempting to think of documentation as a kindness and query tuning as an engineering necessity. That division is misleading. Both are forms of honesty. They require you to admit what the thing is, not just what you want it to do.
A docstring that starts with the real summary shows respect for the reader’s attention. A query that allows indexes to work shows respect for the database’s design. In each case, the structure of the expression aligns with the structure of the system receiving it.
This alignment is the reason these practices scale. Teams with strong documentation do not merely have prettier codebases. They have lower onboarding costs, fewer misunderstandings, and better continuity. Systems with SARGable queries do not merely run faster in benchmarks. They remain predictable under load, easier to reason about, and cheaper to maintain.
The hidden connection is that both are ways of saying: do not force the recipient to reconstruct what you already know.
That is why the most useful habits in software are often the ones that feel almost too simple. Write the obvious thing plainly. Keep the searchable thing searchable. Filter early. Explain first. Preserve structure. Use forms that match function.
Key Takeaways
- Clarity is an optimization, not an aesthetic choice. The clearer the structure, the less work humans and machines must do.
- Avoid unnecessary transformation. If a function, column, or predicate is wrapped in extra processing, you may be hiding the shape the system needs.
- Make intent visible early. In docs, lead with a summary. In SQL, filter early and keep indexed columns usable.
- Standardize simple forms. Consistent conventions reduce cognitive friction and improve tooling, parsing, and maintenance.
- Ask whether the recipient can act without guessing. If not, the problem is not just performance or style, it is expression.
Conclusion: the shortest path is usually the smartest one
We tend to admire cleverness in technical work, but cleverness often reveals itself as extra distance. It adds steps between question and answer, between data and index, between reader and meaning. The wiser choice is usually not the fanciest one. It is the one that keeps the path direct.
That is the surprising unity behind docstrings and SQL optimization: both reward the same discipline of structural respect. Write so the next reader can understand you quickly. Shape queries so the engine can answer efficiently. In both cases, the highest form of sophistication is not complexity, but a kind of disciplined transparency.
The next time you document a function or write a query, ask a better question than "Is this correct?" Ask: What am I making visible, and what am I forcing someone or something else to rediscover?
That question changes not just how you write, but how you think about all readable systems.
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 🐣