Readable Code and Fast Queries Obey the Same Law: Make Meaning Obvious Early

Kai Nguyen

Hatched by Kai Nguyen

Aug 05, 2026

9 min read

84%

0

What if performance and clarity were the same problem?

Most people treat speed and readability as separate virtues. In databases, speed means indexes, execution plans, and avoiding expensive operations. In code, readability means clear names, short summaries, and structure that a future reader can scan quickly. But those are not two different disciplines. They are two expressions of the same deeper principle: good systems reveal their intent as early as possible.

That is why a query that is easy for a database to optimize often looks suspiciously like a sentence a human can understand quickly. And why a docstring that is easy for a human to skim often has the same shape as a query that is easy for a machine to execute. Both reward the same habit: do not force the reader, whether human or engine, to work harder than necessary to discover what matters.

The real cost in software is not computation. It is ambiguity.

A database pays for ambiguity in full table scans, wasted sorting, and unnecessary calculations. A human pays for ambiguity in rereading, reinterpreting, and guessing what the code is supposed to do. The unifying question is not, "How do I make this faster?" or "How do I make this clearer?" It is, "How do I reduce the work required to understand this thing in the right order?"


The hidden rule behind both queries and docstrings: front load the signal

A well tuned query and a well written docstring both obey a simple constraint: put the most useful information where it can be consumed first.

In SQL, that means filtering early, keeping predicates SARGable, using indexes, and avoiding patterns that block efficient access. A condition like WHERE created_at >= '2026-01-01' is more than a technical preference. It tells the engine exactly how to narrow the search. By contrast, wrapping the indexed column in a function or leading with a wildcard forces the database to inspect more rows than necessary because the signal has been hidden behind extra work.

In docstrings, the same idea appears in a different costume. PEP 257 recommends a summary line first, then a blank line, then more detail. That structure is not arbitrary etiquette. It is a compression strategy. The first line gives the immediate answer, the rest expands it only if needed. A one line docstring should fit on one line because obvious things should remain obvious. The reader should not have to excavate meaning that could have been delivered upfront.

This is the deeper symmetry:

  • In SQL, the engine wants the predicate in a form it can act on immediately.
  • In documentation, the reader wants the summary in a form they can absorb immediately.

Both are forms of progressive disclosure. Start with the narrowest, most actionable meaning, then expand outward.

Consider a concrete analogy. Imagine entering a warehouse with 10,000 boxes. If the label on the door says, "Go to aisle 7, shelf 3, blue box, third from the left," you can move directly. If it says, "Walk around until you notice something that seems relevant," you will waste time and probably make mistakes. The database is the warehouse, and the docstring is the label. Both should reduce search, not merely describe the existence of search.


SARGable prose: writing for the reader's execution plan

There is a subtle but powerful way to think about both code and documentation: every piece of writing implies an execution plan.

A SQL optimizer asks: Which conditions can I apply first? Which index can I use? Which operations are cheap, and which force me to scan more than necessary? A human reader asks the same kind of questions unconsciously: What is the point? What can I safely ignore for now? What do I need to understand before the rest will make sense?

That is why SARGable is such a useful mental model beyond databases. Search argument able means the query is structured so the search can happen efficiently. We can borrow that logic for writing:

A piece of writing is SARGable when its meaning can be searched, found, and acted on without unnecessary transformation.

This explains why some documentation feels instantly usable. It does not bury the main idea inside clever phrasing, nested clauses, or context before conclusion. It behaves like a good index. If you need the gist, you find it right away. If you need detail, the detail is there, but it does not get in the way of retrieval.

The opposite is true too. A bad docstring resembles a non SARGable query. It may technically contain the answer, but only after forcing the reader through a function, a detour, or a translation layer. For example:

  • A query that says WHERE YEAR(order_date) = 2026 hides the searchable structure of the data.
  • A docstring that says "Handles stuff related to order processing" hides the searchable structure of the idea.

In both cases, the problem is not absence of information. It is information wrapped in a way that blocks direct access.

This is why the best summaries are often more valuable than the best explanations. A strong summary acts like an index entry. It tells you where to look and what shape to expect. Once that index exists, deeper detail becomes easy to navigate.

Clarity is not the absence of complexity. Clarity is complexity with a usable access path.


Why unnecessary work is the enemy of trust

Performance problems and readability problems both create the same psychological effect: they make the system feel less trustworthy.

A slow query erodes confidence because you cannot tell whether the wait reflects real business complexity or avoidable waste. A vague docstring erodes confidence because you cannot tell whether the function is actually simple or merely described vaguely. In both cases, the user of the system begins to compensate by doing extra work mentally, which is the opposite of what good design should cause.

Think about the difference between these two docstrings:

"""Return the user profile."""

and:

"""Return the canonical user profile for display.

This excludes archived aliases, merges duplicate identities, and applies the
current privacy filter.
"""

The second version is longer, but it is more efficient. It tells you the summary up front, then immediately signals the rules that matter. You do not need to infer whether the function is raw or normalized, public or internal, current or historical. The docstring has already done the search for you.

Now compare that to a query:

SELECT *
FROM users
WHERE LOWER(email) = LOWER('[email protected]')

Versus:

SELECT *
FROM users
WHERE email_normalized = '[email protected]'

The second query is not merely faster because of the index. It is also clearer because it externalizes intent. Instead of asking the database to transform every row on the fly, it names the thing you actually care about. That naming is a form of trust. The system becomes legible because the expensive reasoning has been moved somewhere explicit.

This is the point many teams miss. Readability and performance are not tradeoffs when the design is good. They are often the same asset viewed from different angles. A system that is easy to execute is usually easy to explain, because both qualities come from making structure visible.


The design principle: optimize for directness, not cleverness

The temptation in both SQL and writing is to be clever. Cleverness hides effort. It often feels elegant in the moment, but it usually shifts the burden to someone else later.

A clever SQL query might squeeze everything into one dense statement, but use functions, negations, or wildcards that sabotage index use. A clever docstring might try to sound comprehensive in the first sentence, but end up saying nothing clearly. In both cases, cleverness creates a false economy. It compresses expression while expanding interpretation costs.

The better principle is directness.

Directness means:

  1. State the primary intent first.
  2. Make the common path cheap.
  3. Preserve enough detail for edge cases, but do not let edge cases dominate the front door.
  4. Avoid forcing transformation where a direct representation exists.

In SQL, directness shows up as SARGable predicates, appropriate indexes, and early filtering. In docstrings, it shows up as a summary line, a blank line, and then a deeper explanation only if needed. Both forms respect the reader's time. Both assume that the first job is not to impress, but to orient.

There is a deeper lesson here for technical teams. When a system becomes hard to optimize, it is often because its model has drifted away from its use case. When a function becomes hard to document, it is often because its purpose has become too broad or too implicit. In that sense, performance tuning and documentation are both diagnostics. They reveal where the abstraction no longer matches reality.

If your query needs heroic contortions to run well, maybe the data model is asking the wrong question. If your docstring needs heroic contortions to explain the function, maybe the function is doing too many things.


Key Takeaways

  • Front load the answer. Whether in a query or a docstring, put the most useful information first so the reader or engine can act immediately.
  • Prefer direct representations. Avoid hiding searchable structure behind functions, negations, or vague phrasing when a plain form exists.
  • Think in execution plans. Every piece of writing implies a route from question to answer. Make that route short and obvious.
  • Use summaries as indices. A strong one line summary is not decoration, it is a navigational tool.
  • Treat unnecessary work as a design smell. If humans or machines must keep translating what you mean, the system is losing clarity.

Conclusion: the best systems are easy to look up and easy to look through

We usually talk about SQL optimization as a matter of database mechanics and docstrings as a matter of style. But both are really about the same craft: designing for retrieval.

A query should let the database find what matters without scanning the world. A docstring should let the reader find what matters without scanning the mind of the author. In both cases, the highest form of sophistication is not complexity, but structured obviousness.

That is the reframing worth keeping: good software does not merely compute correctly or explain itself politely. It makes meaning available at the moment it is needed, in the form that can be used immediately. The fastest query and the clearest docstring are cousins because they both do the same noble thing. They remove friction between intent and understanding. And once you see that, you start recognizing a deeper standard everywhere in software: not "How much can this system do?" but "How quickly can it reveal what it means?"

Sources

← Back to Library

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 🐣