Similar issue as described here: False Positive Array.reduce() - Rules and Languages / Report False-positive / False-negative… - Sonar Community
However, I wanted to add my specific, real-world use case where I think compliance with this rule ends up making the code less readable and harder to maintain.
Given an array of numbers, I have to subtract each number in order. For example, [100, 45, 5, 15] should produce 35 ; i.e. 100 - 45 - 5 - 15 = 35. This is simple to do with Array.reduce()
[100, 45, 5, 15].reduce((a, b) => a - b); // 35
If I were to give it a starting value, what should that value be? I can’t use 0 nor could use the first element in the array.
const operands = [100, 45, 5, 15];
operands.reduce((a, b) => a - b, 0); // -165
operands.reduce((a, b) => a - b, operands[0]) // -65
Basically, in order to do this right, I have to remove the first element from my array, and pass that as the initial value:
const operands = [100, 45, 5, 15];
const firstOperand = operands.shift();
operands.reduce((a, b) => a - b, firstOperand); // 35
To me, this adds unnecessary overhead and to something that already works correctly. Not to mention, I had to mutate my original array to get this to work. Naturally I could get around that by copying the array or by chaining a filter before the reduce, but again, this is just more work and ends up being more confusing than something that works out of the box.