for n in list
sum += n
return sum / len(list)
Which will fail but will probably be caught in the code review. Then a cleverer developer might think to write l = len(list)
for n in list
sum += n / l
return sum
Which will also fail but in more subtle ways, hopefully the senior dev will catch it in the code review.Then they correct it to
l = len(list)
for n in list
sum += n / l
rem += n % l
sum += rem / l
rem = rem % l
return sum
But this could further optimized and might still be dangerous. This one might not be fixed at all in the code review.The best algorithm might be Knuth's which is fairly non-trivial and might not even be the fastest/most efficient given certain unique JS optimizations.
Do you want to do this for every 'trivial' function or would you rather import average where (in theory) someone knowledgeable has already done this work and benchmarks for you and you get the best performing algorithm basically for free?