DEV Community

Discussion on: Stop using for loops. Here's why.

Collapse
lukaszahradnik profile image
Lukáš Zahradník • Edited on

Stop using for loops is really bad advice.

You don't event have a real argument to do so.

  • Lack of clarity.

You are right for your examples, but these function can (and will) get more complex and sometimes can be for/while loop more readable. Just look at how unreadable and ugly your function combinationSum is.
Also it's not fair to compare implementation of function with function call in case of sort.

  • Side effects.

No real argument given here, nothing about side effects.

  • Possible mutation.

Side effects are about mutations of outer scope. You divided the same argument into two. The thing is, you can mutate the outer scope from within Array.prototype functions and you even said that later in the article.
By the way, in example doubleArrayValues there is nothing wrong with creating new array, even map will create a new one. Function with the for loop is still pure function.

You shouldn't really stop using for loop, they are more flexible as they are more generalized and in some cases more readable than the prototype functions.
It's important to learn how and where to use loops, map, filter, reduce etc. and use them where they are appropriate. There is very little good usage for reduce.

Collapse
jackshen profile image
Jack Shen Author

You shouldn't really stop using for loop, they are more flexible as they are more generalized and in some cases more readable than the prototype functions.

My point is that for loops shouldn't be the first solution, but rather used on a "as needed" basis. I acknowledge in my article there are circumstances where they may be preferable.

There is very little good usage for reduce.

Could you justify this statement?

Collapse
lukaszahradnik profile image
Lukáš Zahradník

But array prototype functions shouldn't be the first solution either. It just seems from the article that loops are bad and you don't give any reason for that.

The key point is the good word. For me, good code is readable and then performant. Most of the times the code using reduce can be rewritten into the loop form with preserving or even improving readability (the gain of performance is obvious).

It's easier if you watch this video from people at Google, as I agree with most of it.
youtube.com/watch?v=qaGjS7-qWzg

If you have any real life examples of reduce that is just better, share it. I would really appreciate it.

Collapse
blindfish3 profile image
Ben Calder

Agreed on most counts. You don't mention that for loops also allow you to break out of the loop early; which depending on context can provide performance benefits.

There is very little good usage for reduce.

On the contrary, there are some incredibly useful applications for reduce and it can often be used when a normal for loop seems like the only option.

Collapse
lukaszahradnik profile image
Lukáš Zahradník • Edited on

Yeah, I wasn't specific, but I mentioned that loops are more flexible.

I really think that most of the times is reduce used/overused because it's "modern" and "cool".
There sure are use/edge cases, but how often do you come across them?
And they can be usually rewritten into loops easily with preserved or even improved readability.

Thread Thread
idanarye profile image
Idan Arye

+1 about reduce. Consider #3 in dev.to/thejohnstew/5-programming-p...

const exampleValues = [2, 15, 8, 23, 1, 32];
const [truthyValues, falseyValues] = exampleValues.reduce((arrays, exampleValue) => {
  if (exampleValue > 10) {
    arrays[0].push(exampleValue);
    return arrays;
  }

  arrays[1].push(exampleValue);
  return arrays;
}, [[], []]);

It uses reduce to feel functional and hip, but in practice it just mutates the arrays and passing them as is (they are mutated, but it's the same reference) to the next iteration.

But even proper uses of fold/reduce are usually clearer and simpler when done with loops, because the whole point of reduce was to emulate foreach in purely functional languages.