-
Notifications
You must be signed in to change notification settings - Fork 26.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question about 11.1 "Don’t use iterators" #2018
Comments
The issue is that loops themselves are inherently inferior ways to express the intention of your code. They’re you telling the computer (and future readers) how to iterate, instead of you telling the computer what iteration you want done. Loops should be avoided period, not just because of mutation (which is both possible and avoidable with both loops and iteration methods). |
@ljharb, how exactly do you mutate The other part of an argument is not easy to reason about. What does "They’re you telling the computer ... how to iterate" mean, exactly? |
@Raiondesu in that case, the value is a string, so it's immutable. You can mutate http://qr.ae/RNMEGA and https://gist.github.com/robotlolita/7643014 may be helpful reads for understanding why loops less clearly signal intention. |
@ljharb, that's exactly why @ZYinMD's argument is valid: When you write for (const i in iterable) {
//...
} "i" contains a string key, not a reference to a value. keeping this in mind, a right way to replicate for (const i in iterable) {
const item = iterable[i];
} "i", in this case, is immutable. You cannot reassign it. Thanks for the links, though. |
Okay, having read these 2 articles, I can say 2 different things:
So, in the "why" segments it really should've been references to the source material of those conclusions instead of the vague "easier to reason about". I understand, however, that when you become insancely familiar with pros and cons of each approach to write iterative code, it's easy to just say "oh, it's easier to reason about". And it sounds about right as well. Though only for you (and me now) and not for people who haven't read those 2 articles. So why not formulate the reasons to be more descriptive? If yes, then would you accept a PR that reformulates the reasoning based on a particular piece of research (with link, of course)?
What I'm trying to say is... this Style Guide happens to affect JS community in a big way now, as it's being referenced almost anytime when people start discussions on how they should write their code. And you, @ljharb, as a maintainer of this Style Guide, now happen to have a good chunk of responsibility for people's code writing decisions. Because not everybody understands that this style guide is, technically, Airbnb-specific in many ways, as well as your taste-specific in some ways. Because it is presented as a "reasonable approach". Should we continue to make it more reasonable then? |
It's been a while since I submitted this question and email notification brought me back. I actually don't mind abandoning loops, it's not a big deal, but I do have one thought: I agree that As far as mutation is concerned, I have thought through all scenarios of iterating over both arrays and objects using both |
@ZYinMD indeed that's true, but if |
... and then we have browsers struggling to scroll down a bunch of text.
|
@Raiondesu scrolling is an entirely different kettle of fish; the only correct amount of scrolling-related javascript is zero :-) |
Well, yeah. :)
But, unfortunately, not all people rooting for using higher-order functions
think of it this way.
…On Sun, Apr 7, 2019, 05:14 Jordan Harband ***@***.***> wrote:
@Raiondesu <https://github.com/Raiondesu> scrolling is an entirely
different kettle of fish; the only correct amount of scrolling-related
javascript is zero :-)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2018 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AS70VISEiXfm2XTctAKQcc4mgiaq3wRAks5veVR0gaJpZM4baHWD>
.
|
11.1 Don’t use iterators.
Prefer JavaScript’s higher-order functions instead of loops like for-in or for-of.
Why? This enforces our immutable rule. Dealing with pure functions that return values is easier to reason about than side effects.
My question is:
Example 2 is preferred because it can't mutate its argument, however, in example 1, similar restriction can be achieved by using const:
So it seems
for..in
andfor..of
are not inferior than forEach in any other way, as long as I use const. Is that so?Of course there are times when you do want to re-assign
i
which is convenient in forEach, but often you don't.The text was updated successfully, but these errors were encountered: