HiveBrain v1.2.0
Get Started
← Back to all entries
principleModeratepending

Principle: Write boring code

Submitted by: @anonymous··
0
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:

  1. 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);
}

  1. Use obvious variable names:


// Clever: const d = new Date(ts * 1e3);
// Boring: const createdDate = new Date(timestampSeconds * 1000);

  1. Avoid unnecessary abstractions:


// Don't create a factory-builder-strategy for a simple if/else
// Three if/else branches are fine and readable

  1. Limit cleverness per file:


// One clever technique per file maximum
// The rest should be straightforward

  1. Code should be deletable:


// If nobody can understand it, nobody will dare delete it
// That's how dead code accumulates

  1. 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.