What should be the name of the arguments in Array.reduce?
As a matter of best practices in terms of code readability, Which of the following naming convention should be used while naming arguments of the callback in Array.reduce?
const studentAges= [15,16,14,15,14,20]
const sum = studentAges.reduce((accumulator,currentValue)=> accumulator +=currentValue,0);
const sum = studentAges.reduce((sum,age)=> sum+=age,0);
Please refer to the documentation on the reduce parameters and their order.
First param is the
accumulator followed by
As far as the naming convention it is up to you and whatever coding standards you follow or prefer.
I personally prefer what you call the
generic approach since it is consistent between the uses ... but again totally personal preference :)
You can call it accumulator, but as the final value of the accumulator is the result of the reduce function, you can also call it result, which makes it more clear that we are looking at the resultant value. i prefer result as that helps make that point otherwise you have to remember that ... the accumulator is what gets returned. you can of course rename the result to whatever you are looking for out of the function too. So guess it depends on what makes it more readable. my pref though is result, because it also gets the result of each iteration.
I guess it is a matter of preference but I would go for Generic
const sum = studentAges.reduce((item,accumulator)=> accumulator +=item,0);
as there is no need here to be specific. You are passing a callback as argument, and it is the way you use the arguments more than the arguments themselves that makes the callback specific to your usage.
Array.prototype.reduce(), Syntax arr.reduce(callback[, initialValue]) Parameters callback Function… is a function that will be called once for every item in the array. To handle multiple array parameters, you can use arguments. Thanks to that, your function can take N parameters, it is more generic. Then, you can flat all your array parameters, and start to reduce your data. When you will reduce our array, you will create a new array by excluding redundant data.
A look at the Array.reduce() function, The Array type provides a method called reduce() that helps you to reduce an If you pass in the initialValue argument, the reduce() method will assign it to the Instead of variable arguments you should just expect a Seq as an argument to a function. It's very easy to create List or Array in F# and you can directly inline those creations with a function call. You don't get problems if you already have a collection.
.reduce((item,accumulator)No, the accumulator is the first argument to the
reducecallback. Also, your second code doesn't have any
- @CertainPerformance edits made in the question.
- Specific is better
- The question is opinion-based, but I think being as specific as you can is best for readability. So, for example, your
sumin the second code's
reduceisn't actually the sum, it's only a partial sum of the ages so far. I think I would use:
const studentAgeSum = studentAges.reduce((studentAgesSubtotal, studentAge)=> studentAgesSubtotal + studentAge, 0);If you see
studentAgesSubtotal, you immediately know what that means. If you see
accumulator, you have no idea. If you see
sum, you only have some idea.
- @CertainPerformance you have a valid point in favor of specific approach, however, using generic one can be preferred when you want to keep things consistent throughout the code, anyone who knows about reduce would know what accumulator means.Thanks for your input though
- That was a mistake that I have corrected now, your answer makes sense to me, thanks.