Sparse is better than dense
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.
Give each idea its own line. Packing code tight saves the author a few lines, and every reader after that has to slow down to untangle it.
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 it is 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
Now the answer has its own line. The discount comes off the subtotal, and tax goes on after.
Sparse code is also easier to review. Say coupons get capped at 50% off. In the dense version, the diff replaces the whole line, and the reviewer has to compare two long expressions to find the change. In the sparse version, the diff is one line:
-percent_off = coupon.percent_off if coupon else 0
+percent_off = min(coupon.percent_off if coupon else 0, 50)
Sparse doesn’t mean verbose. Use short functions with clear names, and blank lines as paragraph breaks between groups of related statements.
Experienced developers feel the pull toward compression most. Chaining three operations into one expression feels like mastery. Usually you’re saving yourself a minute now and costing everyone else more later. Generated code makes this worse, since models write for correctness, not readability. “Sparse is better than dense” comes from the Zen of Python, but it applies to any language.