Readable over clever
Code is read far more often than it's written, so optimise for the reader.
A condensed one-liner feels great to write. It feels a lot less great to read six months later, during an incident, when you’re not the person who wrote it.
An example
Here’s a perfectly valid way to count active users per region:
const counts = users.reduce<Record<string, number>>(
(acc, u) =>
u.active ? { ...acc, [u.region]: (acc[u.region] ?? 0) + 1 } : acc,
{},
);
And here’s one that someone skimming the file can follow top to bottom:
const counts: Record<string, number> = {};
for (const user of users) {
if (!user.active) {
continue;
}
counts[user.region] = (counts[user.region] ?? 0) + 1;
}
The second version is longer, but every line does one thing. It’s easier to step through in a debugger, too.
A useful test
Ask yourself: could someone unfamiliar with this code say what it does without reconstructing it in their head first? If not, it’s worth another pass.