Did you notice what happened? I added a = in the for condition, the new result is and will always will be NaN instead of the expected sum.
This is a reason why I don't prefer the first approach. There are tons of ways to loop in JS, I consider the "classic" for loop to be the worst option in most cases due to its verbose-ness and ease of it turning into a foot gun.
Now its possible this is the only for loop known by the developer and thus it is used. This is why I think its more about context than the actual code when it comes to choices like this. If the entire dev team is all about the "functional life" then more power to you. If the entire code base uses .reduce 1 liners, then it might make sense to stick with it for better or worse. If the entire code base looks like it was written back in ES3, I'd question why, it could be a technical reason, or a "human" reason. Its possible no one knows how or what .reduce is so using it out of the blue would probably be a bad idea 😆
I personally believe in leveraging as much of the existing JS API as possible when it comes to code like this. As such I'd go with this version of the code:
I'm not sure it's all that easy to turn into a foot gun if you understand how for loops work :). Sorry, this doesn't really sway me much, and I feel like you're talking about something different here.
I'm not sure it's all that easy to turn into a foot gun if you understand how for loops work
If you know its a foot gun it usually stops being a foot gun.
I think we are assuming something we shouldn't.
If we are being totally truthful, its not a given the developer knows how the classic for loop works. In the same fashion its not a given the developer knows how reduce works. You could argue all JS developers know about all types of loops, but then we all know that's false at some level.
Its not about one loop being better than the other in terms of being understood. Its the idea one loop is better than the other for everyone and anyone. It really depends on who the audience is.
I don't like being this nitpicky, but I wanted to bring it up because I feel like that's the goal of the post right? Yes I know how the classic for loop works. No I don't use it and no I don't think its easier to read.
Is there a right answer? NO, but I think there is a wrong way to go about thinking about it. Where we assume what we feel is "right" is right for everyone.
Its not about the code per say, its about the people reading the code.
Depends right? Here's the code again:
Did you notice what happened? I added a
=
in thefor
condition, the new result is and will always will beNaN
instead of the expected sum.This is a reason why I don't prefer the first approach. There are tons of ways to loop in JS, I consider the "classic" for loop to be the worst option in most cases due to its verbose-ness and ease of it turning into a foot gun.
Now its possible this is the only
for
loop known by the developer and thus it is used. This is why I think its more about context than the actual code when it comes to choices like this. If the entire dev team is all about the "functional life" then more power to you. If the entire code base uses.reduce
1 liners, then it might make sense to stick with it for better or worse. If the entire code base looks like it was written back in ES3, I'd question why, it could be a technical reason, or a "human" reason. Its possible no one knows how or what.reduce
is so using it out of the blue would probably be a bad idea 😆I personally believe in leveraging as much of the existing JS API as possible when it comes to code like this. As such I'd go with this version of the code:
I'm not sure it's all that easy to turn into a foot gun if you understand how for loops work :). Sorry, this doesn't really sway me much, and I feel like you're talking about something different here.
If you know its a foot gun it usually stops being a foot gun.
I think we are assuming something we shouldn't.
If we are being totally truthful, its not a given the developer knows how the classic
for
loop works. In the same fashion its not a given the developer knows howreduce
works. You could argue all JS developers know about all types of loops, but then we all know that's false at some level.Its not about one loop being better than the other in terms of being understood. Its the idea one loop is better than the other for everyone and anyone. It really depends on who the audience is.
I don't like being this nitpicky, but I wanted to bring it up because I feel like that's the goal of the post right? Yes I know how the classic
for
loop works. No I don't use it and no I don't think its easier to read.Is there a right answer? NO, but I think there is a wrong way to go about thinking about it. Where we assume what we feel is "right" is right for everyone.
Its not about the code per say, its about the people reading the code.
Quoting on this again.
This is really because this feels familiar and most developers are used to C like syntax.