Do not confuse simple with unfamiliar
Things can appear complicated if you are not familiar with them. This can lead to simple solutions being misinterpret as over engineering.
Example
Section titled “Example”If you are not accustomed to reading and using standard algorithms, perhaps because you have a long history of reading and writing raw for loops instead, you might argue that a raw for loop such as this:
int sum = 0;for (int i = 0; i < values.size(); ++i) { if (values[i] > 0) { sum += values[i] * scale; }}is simpler and easier to read than a modern approach using a standard algorithm:
const int sum = std::accumulate( values.begin(), values.end(), 0, [scale](int acc, int v) { return v > 0 ? acc + v * scale : acc; });std::ranges::transform(elements, output.begin(), convert);However, don’t let a familiarity bias fool you.
In fact, the raw loop has a higher cyclomatic complexity (2 vs. 1)1. Usage of std::transform expresses intent in a single statement, while the loop forces readers to parse its mechanics, increasing cognitive load.
See also:
- Simplicity vs Familiarity: When evaluating something new, avoid prejudice simply because it’s unfamiliar.
- Refactoring Raw For-Loop C++ to Modern Standards
Footnotes
Section titled “Footnotes”-
Of course, under the hood
transformalso implements a loop. The total execution complexity is the same, but the function callingtransformis simpler and easier to parse mentally. However, it would be fair to argue that abstractions such astransformwill obscure the true complexity of an algorithm. ↩