principleModeratepending
Principle: Write code for the reader, not the writer
Viewed 0 times
readabilitynamingclean codeguard clausescode reader
Problem
Code is written once but read many times. Optimizing for writing speed produces code that's expensive to maintain.
Solution
Prioritize readability in every decision:
Naming:
- Bad:
- Bad:
- Bad:
Structure:
The test: Can a new team member understand this code in 30 seconds? If not, simplify it.
Naming:
- Variables: describe what it IS, not how it's computed
- Bad:
filteredSortedItems Good: availableProducts- Functions: describe what it DOES from caller's perspective
- Bad:
processData() Good: calculateMonthlyRevenue()- Booleans: should read as true/false questions
- Bad:
flag, status Good: isVisible, hasPermissionStructure:
- Extract complex conditions into named booleans
// Hard to read
if (user.age >= 18 && user.verified && !user.banned && user.credits > 0)
// Easy to read
const canPurchase = user.age >= 18 && user.verified && !user.banned;
const hasCredits = user.credits > 0;
if (canPurchase && hasCredits)
- Return early to reduce nesting
// Hard: deeply nested
function process(user) {
if (user) {
if (user.active) {
if (user.hasPermission) {
// actual logic buried here
}
}
}
}
// Easy: guard clauses
function process(user) {
if (!user) return;
if (!user.active) return;
if (!user.hasPermission) return;
// actual logic at top level
}
The test: Can a new team member understand this code in 30 seconds? If not, simplify it.
Why
Code is read 10x more than it's written. The few seconds saved typing a shorter name or skipping a refactor cost minutes every time someone reads it.
Context
All software development
Revisions (0)
No revisions yet.