Current writing
Earlier writing / Software engineering

Accidental Complexity

Revised October 19, 2026. What changed

Essential complexity comes from the problem you must solve. Accidental complexity comes from the representation, tools, or organization you chose. The distinction is contextual: a constraint that is unnecessary in one application may be essential in another.

Make the calculation visible

Take the first k prime numbers in a supplied list and sum their square roots. The list order matters; the example does not generate an unbounded sequence of primes. Here is a complete definition of the predicate and calculation:

static boolean prime(int n) {
        if (n < 2) return false;
        for (int d = 2; d <= n / d; d++) if (n % d == 0) return false;
        return true;
    }
    static double rootSum(List<Integer> values, int k) {
        if (k < 0) throw new IllegalArgumentException("k must be nonnegative");
        return values.stream().filter(LegacyExamples::prime).limit(k)
            .mapToDouble(Math::sqrt).sum();
    }

For [1, 2, 4, 9, 5] and k = 2, the result is sqrt(2) + sqrt(5). Zero requests return zero; negative requests are rejected. If fewer than k primes exist, this contract sums those available. The division bound avoids overflow from calculating d * d.

Choose the clearest representation

The pipeline separates selection, truncation, transformation, and aggregation. An ordinary loop can express the same contract well, especially when early exits or diagnostics matter. “Declarative” is not a guarantee of simplicity. Prefer the version whose order, edge cases, and costs are easiest for the next reader to check.

The earlier example used an invalid method reference. The tested version uses Math::sqrt and a defined integer primality predicate.

The workbench includes the complete LegacyExamples.java program, imports, assertions, and a run command: java LegacyExamples.java with JDK 17 or later. The fragments below are extracted from that same tested source.

Search titles, article text, and concepts.

Start typing to search the writing.