The Art of Readable Code
Code is read far more often than it is written. Here are the habits that make a real difference.
Code is written once and read dozens of times — by your teammates, your future self, and the AI assistant you're inevitably going to ask for help. Readability isn't a nice-to-have; it's a core engineering skill.
Naming is everything
The most impactful thing you can do for readability is choose good names. Good names are:
- Specific —
getUserByIdbeatsgetUser, which beatsget - Honest — a function named
calculateTotalshouldn't have side effects - Proportional to scope — a loop counter can be
i; a module-level variable should not be
Small functions
A function should do one thing. If you can't describe what it does in one sentence without using "and", it should be split up.
// Hard to reason about
function processOrder(order: Order) {
validateOrder(order);
applyDiscounts(order);
calculateTax(order);
chargeCard(order);
sendConfirmationEmail(order);
}
// Better: each step is isolated, testable, and named
const pipeline = [
validateOrder,
applyDiscounts,
calculateTax,
chargeCard,
sendConfirmationEmail,
];
pipeline.forEach(step => step(order));
Comments explain why, not what
Don't explain what the code does — the code already does that. Explain why it does it.
// Bad
// Add 1 to the count
count += 1;
// Good
// The API is 1-indexed, so we offset before sending
count += 1;
Consistency over cleverness
A codebase where every developer has the same style — even if it's not your preferred style — is far more readable than one where each file feels like a different author's essay.
This is why linters, formatters, and code reviews exist.
Conclusion
Readable code is an act of empathy. Write it as if the person reading it at 2am trying to fix a production bug has never seen your codebase before. Because they might not have.