Sparse Is Better than Dense
tl;dr
Packing six things into twelve lines makes every reader take the code apart before reasoning about it, and diffs hide changes inside long expressions. Give each idea its own line, use short named functions and blank lines as paragraph breaks, and resist clever one-liners, including dense AI-generated code.
Key takeaway: Put one idea on each line and group related statements with blank lines, so a reader and a diff can see each change.
Density is a readability tax
Dense code feels efficient. Everything on one line, no blank lines between blocks, maximum logic per screen. It looks like you accomplished a lot. But reading it requires holding the entire context in your head simultaneously, and that’s where bugs hide.
When a function does six things in twelve lines, the reader has to mentally decompose it before they can reason about any single part. That decomposition is work. Every person who reads that code pays that tax. The author pays it once; the readers pay it forever.
What sparse actually means
Sparse doesn’t mean verbose. It doesn’t mean adding blank lines randomly or padding everything with comments. It means giving each idea room to breathe:
- One idea per line. If a line does two things, split it.
- Short functions with clear names. Let the function name carry the “what” so the body only needs to carry the “how.”
- Blank lines as paragraph breaks. Group related statements. Separate unrelated ones. The visual structure should mirror the logical structure.
- Avoid deeply nested logic. Early returns, guard clauses, and extracting helper functions all reduce nesting and make the happy path obvious.
The goal is code where intent jumps out at you instead of hiding behind clever one-liners.
Here’s an order total that does five things in one expression:
# checkout.py: before
total = sum(i.price_cents * i.qty for i in items if not i.refunded) * (1 - (coupon.percent_off if coupon else 0) / 100) + (vat(items) if region == "EU" else 0)
To check whether the discount applies before or after tax, you have to count parentheses. Here’s the same calculation with one idea per line:
# checkout.py: after
active_items = [item for item in items if not item.refunded]
subtotal = sum(item.price_cents * item.qty for item in active_items)
percent_off = coupon.percent_off if coupon else 0
discount = subtotal * percent_off / 100
tax = vat(items) if region == "EU" else 0
total = subtotal - discount + tax
The answer is now on its own line: the discount comes off the subtotal, and tax is added after.
The compression trap
There’s a strong pull toward compression, especially among experienced developers. You learn a language well enough to chain three operations into one expression, and it feels like mastery. Sometimes it is. But more often it’s trading your time now for everyone else’s time later.
This is a close cousin of over-engineering: both are cases where the author optimizes for the wrong thing. Over-engineering optimizes for hypothetical futures. Dense code optimizes for brevity at the cost of clarity. Neither serves the people who maintain the code.
Sparse code scales better
In a codebase touched by many people (or many agents), sparse code has a compounding advantage. It’s easier to review and easier to merge. When each line does one thing, a changed line in a pull request tells you exactly what changed and why. Dense code hides changes inside complex expressions where a reviewer has to parse the whole thing to find the difference.
Say the business caps coupons at 50% off. In the dense version, the diff replaces the whole line:
-total = sum(i.price_cents * i.qty for i in items if not i.refunded) * (1 - (coupon.percent_off if coupon else 0) / 100) + (vat(items) if region == "EU" else 0)
+total = sum(i.price_cents * i.qty for i in items if not i.refunded) * (1 - min(coupon.percent_off if coupon else 0, 50) / 100) + (vat(items) if region == "EU" else 0)
In the sparse version, the diff touches the one line that changed:
-percent_off = coupon.percent_off if coupon else 0
+percent_off = min(coupon.percent_off if coupon else 0, 50)
This matters even more now that code quality has to survive both human and AI authorship. AI-generated code tends toward density because models optimize for correctness, not readability. Sparse code is the antidote: it forces structure that makes intent visible regardless of who (or what) wrote it.
The Zen got it right
The Zen of Python isn’t really about Python. It’s about software. “Sparse is better than dense” is a design principle that applies to any language, any codebase, any team. The same goes for the line between complex and complicated. It’s a universal design principle hiding in a Python style guide. Give your code room to breathe. Your future readers will thank you.