r/programming • • Jun 09 '16

jQuery 3.0 Released!

https://blog.jquery.com/2016/06/09/jquery-3-0-final-released/
1.1k Upvotes

354 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Jun 10 '16

[deleted]

11

u/tech-ninja Jun 10 '16

But then your intent is not clear anymore. Anyway I wouldn't be concerned about the speed of forEach vs map.

16

u/EternalNY1 Jun 10 '16

Anyway I wouldn't be concerned about the speed of forEach vs map.

A lot of people would be!

And therein lies the problem.

Programmers working with JavaScript sometimes seem like they are running the particle colliders at CERN.

When one image on your page is bigger than jQuery, and you are worried about having to "bring in all of jQuery when we only use half of it", you need to take a deep breath and start again.

Who does have to worry about this? The Google's and Facebook's of the world, where a change that is a few bytes more expensive can cost millions of dollars.

Who doesn't? Almost everyone else.

3

u/[deleted] Jun 10 '16

exactly, almost never becomes a problem and I build some pretttttty fucking meaty clients.

6

u/enkideridu Jun 10 '16

Please don't use maps to perform mutations

2

u/Labradoodles Jun 10 '16

So I understand the logical reasoning behind not doing mutations with a functional.method with an emphasis on immutability but in this case I don't see much if a problem would you care to elaborate what's wrong with it?

4

u/puhnitor Jun 10 '16

The intent is no longer clear. .map has a very clear intent - give me a new array with some transform performed to all the elements. Don't mutate the original array.

.forEach is the opposite.

3

u/kqr Jun 10 '16

It communicates conflicting intentions. The map says "any side effects are a mistake and should be put in the bug tracker" while the loop body says "I'm nothing but a side effect." That kind of thing makes the code harder to read for the next person two years down the line.

2

u/[deleted] Jun 10 '16

maps return a new array, no need to mutate.

1

u/enkideridu Jun 10 '16 edited Jun 11 '16

We're talking about adding classes to DOM elements here, how do you do that with a map and without mutation?

1

u/vplatt Jun 10 '16

This is Javascript not Haskell. MUTATE ALL THE THINGS!

(Seriously, I get it. But I strongly suspect it's going to be the "best practice" down the road.)

1

u/enkideridu Jun 11 '16

I don't see how? Could you share your insight on why using map when you should be using forEach might become a best practice?

1

u/vplatt Jun 11 '16

No particular reason other than the fact that I can be a little cynical and part of me believes the community will opt for map simply because it's shorter and "cooler" to use functional programming techniques. The fact that there is nothing in Javascript (that I know of anyway) to enforce referential transparency means that use of the FP techniques will likely be little more than lip service to the concepts.

I'm pretty hard on the Scala and Haskell communities when it comes to being able to make FP practical and accessible for everyday use, but at least they understand how to get the benefits of FP. I currently do not have any such confidence in the Js community.

2

u/enkideridu Jun 11 '16 edited Jun 11 '16

I'm not sure, I might quite possibly have misinterpreted (apologies if that's the case), but just in case, the motivation behind not using map and reduces to perform mutations isn't out of an idealistic "functional programming is good" or "mutations are bad" dogma. It's for the benefit of making your code easier to understand for yourself and others

Specifically, it's an implicit promise that the code inside the function, no matter how complex, can be safely ignored as long as the value it returns is as expected. There is huge value in being able to rely on the (albeit implicit) referential transparency for maps and reduces in the amount of code you can skip when you're combing for bugs or trying to understand a section of a program someone else wrote.

And I can tell you of one way of enforcing referential transparency - code review :)

I've run into a few javascript programmers who've made the same mistake with misusing map (2 juniors and one self-taught rockstar), but after I've explained it the responses were always immediately "that makes sense". That gives me hope that while it may be an easy mistake to make, it won't get touted as a best practice because people will understand why they shouldn't be doing it as soon as you explain it to them, and hopefully pass it forward :)

2

u/vplatt Jun 11 '16

All great points. I don't have anything to add to that. Hopefully you're right about that.

2

u/[deleted] Jun 10 '16

in client work, how often is forEach actually an issue? use the most succing code then take a quick look at the profile against a large set of data.

for less code, easier to read, and of course if isn't causing jank, it makes sense to go with forEach.

0

u/[deleted] Jun 10 '16

[deleted]

1

u/Benmjt Jun 10 '16

Succinct, maybe?

1

u/[deleted] Jun 10 '16

Succinct. Phone shit.

2

u/EternalNY1 Jun 10 '16

use the most succing code then take a quick look at the profile against a large set of data.

I would say, always ... write the most READABLE code that you still feel is performant, then profile it. If it's fine from an overall performance standpoint, you are good.

That's it! It's not rocket science (well, it might be, but usually not if we're talking about things like JavaScript).

You don't even need to comment it! It just makes sense from looking at it and is fine. Who cares if it's 3 ms slower than the other version that is 10 lines longer? If that is not impacting your users, move on.