question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`prefer-for-of` should include some Array methods (forEach, reduce)

See original GitHub issue

Please describe what the rule should do:

Rewriting forEach and reduce loops to for-of increases their readability.

  • forEach isn’t beneficial to anything unless you’re really into fp and one-liners. makes you create a single-use function when you might not need one, especially when nesting it.
  • reduce tends to create backwards logic, where the starting value of the loop is placed at the end. Example thread: https://twitter.com/jaffathecake/status/1213077702300852224

Other methods like map and some on the other hand are very effective and readable, so they should be left alone.

Prior art:

What category of rule is this? (place an “X” next to just one item)

[ ] Warns about a potential error (problem) [x] Suggests an alternate way of doing something (suggestion) [ ] Enforces code style (layout) [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

👎 Examples of incorrect code for this rule:

[1, 2, 3].forEach(item => {
	console.log(item);
});

const accumulator = [1, 2, 3].reduce((accumulator, item) => {
	accumulator += item;
	return accumulator;
}, 0);

👍 Examples of correct code for this rule:

for (const item of [1, 2, 3]) {
	console.log(item);
}

let accumulator = 0;
for (const item of [1, 2, 3]) {
	accumulator += item;
}

// async functions are allowed
[1, 2, 3].forEach(async item => {
	throw new Error(item);
});

Why should this rule be included in ESLint (instead of a plugin)?

You already have prefer-for-of in typescript-eslint (which shouldn’t be there in the first place given it’s about a very basic feature of the language and unrelated to types)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:20 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Jan 8, 2020

Then, just to confirm, this is a proposal for a new ESLint core rule which would disallow .forEach(), .reduce() and for loop when any of these can be replaced with a for-of loop?

1reaction
timdpcommented, Jan 6, 2020

Sure. Just saying there are a lot of reasons why one might not want this rule.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why and when to use forEach, map, filter, reduce, and find in ...
I thought it would be useful to provide an explanation of when to use the common array methods. .map() , .filter() , .reduce()...
Read more >
eslint-plugin-github/array-foreach.md at main
Here's a summary of why forEach is disallowed, and why we prefer for...of for almost any ... However not all "iterables" have access...
Read more >
Why should forEach be preferred over regular iterators?
This reasoning in Airbnb style guide applies to array methods that are used for immutability, which are filter , map , reduce ,...
Read more >
Array Methods Explained : Filter vs Map vs Reduce vs Foreach
Basically forEach works as a traditional for loop looping over the array and providing you array elements to do operations on them. okay!...
Read more >
When should you use ForEach instead of map, reduce or filter?
You have an array of emails a user has clicked on (an array of IDs). They press a button. You delete those emails...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found