DEV Community

Discussion on: How to use the `reduce` Method in JavaScript (and React)

Collapse
 
mindplay profile image
Rasmus Schultz

Okay, that's the how.

The bigger question for me has always been why?

A plain for-loop is almost always both faster and more readable. For one, you can name your total variable, which helps people understand what your code is trying to do.

Even in situations where I'm forced to use an expression (e.g. JSX) I prefer adding an extra function.

The only arguments I've heard in favor of reduce is things like "feels more functional", which just never really outweighs priorities like simplicity and speed for me.

If there's a case where a reduce call doesn't easily refactor into a simpler, faster, more readable procedural version of the same thing, I haven't seen it yet. 🤷‍♂️

Collapse
 
yogesnsamy profile image
yogesnsamy

Yeah I've wondered about the why too and I finally learnt about reduce thanks to a React exercise that required its use. :)

But now that I've tried it, I do find the code shorter and simpler to write though I agree it's not very readable.

With the for-loop, my code is much longer like the following. Any feedback/suggestion on this?

 const totalz2 = () => {
    let total2 = 0;
    for (let i = 0; i < props.parts.length; i++) {
      total2 += props.parts[i].exercises;
    }
    return total2;
  };
Collapse
 
mindplay profile image
Rasmus Schultz

I'd use for-of rather than for-in, since you don't care about the index - introducing it as a variable means the reader has to figure out what's important and filter that out first.

I would also pass props.parts to the function, to illustrate the fact that this function doesn't care about anything else - seeing the function signature, a person understands right away what the dependencies of that function are; sometimes that can answer their question without making them scan through the function body to figure out what input the function requires.

In general, I'd rather have twice as many lines and have something everyone can immediately read and understand (and modify without breaking it!) without a tutorial - and let a minifier do the minifying.

On the issue of inlining vs functions, I lean heavily towards functions, because glazing over a large render-function, I'd rather see descriptive names like countTotalExercises as opposed to an tangle of inline expressions - the reader then knows right away what you're doing and doesn't need to be burdened with how you're doing it, before they need to.

And likely this was just an example, but I'd use more descriptive names. 😉

Thread Thread
 
yogesnsamy profile image
yogesnsamy

Thank you so much for taking the time to share your valuable feedback. I like how you prioritize readability over other features. Keeping this in mind moving forward.

Thread Thread
 
nadabk profile image
nad-abk • Edited

@yogesnsamy sorry but no, i prefer by far your way of writing tutorials !! it's so refreshing actually !! and it's straight to the point and step by step thing is ... perfect !!! and the fact that i might have to wonder what every line of code does actually helps a lot !! definitely going to follow you and read every tutorial you'll write !

Thread Thread
 
yogesnsamy profile image
yogesnsamy

You're too kind. Thank you for making my day!