Blog

Clean Code Is Overrated When You Need Performance

measure before purity
Beautiful organized toolbox beside a greasy chaotic high-performance race engine

Clean Code is good advice for most business logic. Most.

There is remaining 5% where pretty abstraction burns CPU, allocates garbage, and loses money. In hot path, performance is feature.

Do Not Start Ugly

First write clear code. Add tests. Measure. Find actual bottleneck. Then optimize only bottleneck.

Premature ugliness is amateur. Measured ugliness is engineering.

Stream Version

BigDecimal total = transactions.stream()
    .filter(Transaction::settled)
    .map(Transaction::amount)
    .reduce(BigDecimal.ZERO, BigDecimal::add);

Readable. Fine for many cases.

But in high-volume path with millions records, streams can allocate, create lambdas, and hide costs. BigDecimal itself is heavy too.

Hot Path Version

long totalMinorUnits = 0L;
for (int i = 0; i < transactions.size(); i++) {
    Transaction tx = transactions.get(i);
    if (tx.settled()) {
        totalMinorUnits += tx.amountMinorUnits();
    }
}

Less elegant. Faster if profiler says this loop matters. Uses long minor units instead of BigDecimal allocation.

Architecture Still Matters

Do not optimize controller formatting while database query scans table. Do not hand-roll parser because you dislike library. Performance work starts with measurement.

Java Flight Recorder, async-profiler, allocation profiling, database plans. Tools before opinions.

Rule Breaking Needs Fence

When code is intentionally ugly for speed, isolate it.

/** Hot path. Keep allocation-free. Benchmark before changing. */
final class SettlementAccumulator { ... }

Add benchmark. Add tests. Explain constraint. Future developer should not “clean” it into slower version.

Competitive Programming Lesson

CP teaches that constant factors matter. Enterprise teaches that humans maintain code. Real senior balances both.

Clean code everywhere is dogma. Fast chaos everywhere is also dogma.

Final Rule

Write clean by default. Break rules only when profiler gives reason and tests give safety.

Pretty slow code in trading path is not moral victory.