gtap-dev / javascript

JavaScript Style Guide
MIT License
0 stars 0 forks source link

Replaced "Don't use iterators" rule with "Prefer higher-order functions" #22

Open mjomble opened 2 months ago

mjomble commented 2 months ago

Based on #6

I also noticed that previously, examples like this were labelled as "good" simply for using .forEach instead of for-of:

const increasedByOne = [];
numbers.forEach((num) => {
  increasedByOne.push(num + 1);
});

Based on the issue discussion, it seems to me that our main goal with this rule is to push developers to use functional programming methods like .map(), .filter() etc instead of iteration with side effects.

Considering that, .forEach() seems just as bad as any for loop in this example. I've updated the examples to demonstrate the use of more array methods and labelled their iteration variants as bad even if they use .forEach.

When side effects are unavoidable, .forEach doesn't really make anything more immutable or pure compared to for-of. But I did include an example where it's preferable to use it.