Beautiful is better than ugly
Key takeaway: Treat readable code as a maintenance requirement, and let a formatter handle style so review time goes to names and structure.
Ugly code works, which is why it survives. Whoever reads it next pays the cost. The Zen of Python puts “Beautiful is better than ugly” first, and much of the rest reads like a definition of it: explicit, simple, flat, sparse, readable. None of that is specific to Python.
Here’s a function that does its job:
def chk(d):
r = []
for k in d.keys():
if d[k]["status"] == "active":
if d[k]["days_overdue"] > 30:
r.append(k)
return r
And the same function after ten minutes of care:
OVERDUE_THRESHOLD_DAYS = 30
def overdue_active_accounts(accounts):
return [
account_id
for account_id, account in accounts.items()
if account["status"] == "active"
and account["days_overdue"] > OVERDUE_THRESHOLD_DAYS
]
Same behavior. The second one tells you what it’s for, names the magic number, and drops a level of nesting. Nobody will file a bug against the first. People will just read it slower, misread it sometimes, and work around it instead of changing it.
Beautiful usually means boring. A clever one-liner is fun to write and hard to read. The beauty I mean is closer to good typography: consistent names, and code shaped like the problem. It reads at about the speed of prose.
Let a formatter handle the surface stuff. Black, Ruff, gofmt, Prettier, RuboCop: pick one, run it in CI, and stop arguing about quotes. That frees review for what a formatter can’t see, like a function named process that does four things.
That matters more as models write more code. Generated code tends to be correct and long-winded, with vague names and checks for cases that can’t happen. So write the version you’d want to inherit.