principleModeratepending
Principle: Write boring code
Viewed 0 times
boring-codereadabilitysimplicitymaintainabilityclarity
Problem
Clever code with advanced language features, terse syntax, and creative patterns is hard for others (and future you) to understand and maintain.
Solution
Optimize code for reading, not writing:
// Clever:
const result = items.reduce((a, x) => (a[x.type] = [...(a[x.type]||[]), x], a), {});
// Boring (and clear):
const result = {};
for (const item of items) {
if (!result[item.type]) result[item.type] = [];
result[item.type].push(item);
}
// Clever: const d = new Date(ts * 1e3);
// Boring: const createdDate = new Date(timestampSeconds * 1000);
// Don't create a factory-builder-strategy for a simple if/else
// Three if/else branches are fine and readable
// One clever technique per file maximum
// The rest should be straightforward
// If nobody can understand it, nobody will dare delete it
// That's how dead code accumulates
// A common pattern done conventionally is better than
// a novel pattern done cleverly
Litmus test: Can a junior developer on the team understand
this code in under 5 minutes? If not, simplify it.
- Use the most straightforward approach:
// Clever:
const result = items.reduce((a, x) => (a[x.type] = [...(a[x.type]||[]), x], a), {});
// Boring (and clear):
const result = {};
for (const item of items) {
if (!result[item.type]) result[item.type] = [];
result[item.type].push(item);
}
- Use obvious variable names:
// Clever: const d = new Date(ts * 1e3);
// Boring: const createdDate = new Date(timestampSeconds * 1000);
- Avoid unnecessary abstractions:
// Don't create a factory-builder-strategy for a simple if/else
// Three if/else branches are fine and readable
- Limit cleverness per file:
// One clever technique per file maximum
// The rest should be straightforward
- Code should be deletable:
// If nobody can understand it, nobody will dare delete it
// That's how dead code accumulates
- Use well-known patterns:
// A common pattern done conventionally is better than
// a novel pattern done cleverly
Litmus test: Can a junior developer on the team understand
this code in under 5 minutes? If not, simplify it.
Why
Code is read 10x more than it's written. Boring code is easy to understand, debug, modify, and delete. Clever code is a maintenance burden.
Revisions (0)
No revisions yet.