2

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]

  • Generic

    const sum = studentAges.reduce((accumulator,currentValue)=> accumulator +=currentValue,0);

  • Specific

    const sum = studentAges.reduce((sum,age)=> sum+=age,0);

Bilal Alam
  • 874
  • 10
  • 26
  • 2
    `.reduce((item,accumulator)` No, the accumulator is the **first** argument to the `reduce` callback. Also, your second code doesn't have any `item` defined – CertainPerformance Aug 08 '18 at 06:53
  • @CertainPerformance edits made in the question. – Bilal Alam Aug 08 '18 at 07:03
  • Specific is better – Shishir Arora Aug 08 '18 at 07:06
  • The question is opinion-based, but I think being as specific as you can is best for readability. So, for example, your `sum` in the second code's `reduce` isn'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 Aug 08 '18 at 07:06
  • @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 – Bilal Alam Aug 08 '18 at 07:12

3 Answers3

2

Please refer to the documentation on the reduce parameters and their order.

First param is the accumulator followed by currentValue, currentIndexOptional and array.

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 :)

Akrion
  • 18,117
  • 1
  • 34
  • 54
1

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.

Mickey Puri
  • 835
  • 9
  • 18
0

I sometimes go for a hybrid approach by adding acc to the accumulator variable name, eg if the result is an array. In this way you can distinguish between the accumulator in the reduce and the result while still adding meaning to both variable names.

const students = [{
    name: 'Joe',
    city: 'Wellington'
  },
  {
    name: 'Jane',
    city: 'Auckland'
  },
  {
    name: 'Jack',
    city: 'Auckland'
  },
  {
    name: 'Jenny',
    city: 'Wellington'
  },
  {
    name: 'James',
    city: 'Wellington'
  }
];

const wellingtonStudents = students.reduce((wellingtonStudentsAcc, student) => {
  if (student.city === 'Wellington') {
    wellingtonStudentsAcc.push(student);
  }
  return wellingtonStudentsAcc;
}, []);

console.log(wellingtonStudents);
Johan Maes
  • 1,161
  • 13
  • 13