DEV Community

Discussion on: Improve your JS skills with those tips #2

Collapse
leob profile image
leob

What's the problem with using "splice" to delete an element from an array? Works even in ES5, and I can't imagine that the "functional" approach (in essence copying the whole array except one element) would be faster ...

Collapse
silverium profile image
Soldeplata Saketos

once you work with pure functions, and data is being transformed and used in a long sequence of functions, moving to store, to state, to helpers... functions that are changing "in place" arrays are very dangerous. Splice, Sort, Push, Pop, Unshift... they need to be used only in the "data formation" phase, but never as part of any data transformation, as they will lead to side effects very fast

Collapse
leob profile image
leob

Yeah but the point of the original author was that "delete" is slow, and therefore you need to use the approach that he proposed ... to which I replied "what if you don't use delete but splice", probably the argument about performance goes away then.

The FP (functional programming/immutable) approach is cool, but there's still a legit case to be made for the old fashioned way - it's only "dangerous" if you do it wrong, heaps of code are written this way which are perfectly fine.

Thread Thread
silverium profile image
Soldeplata Saketos

I said "dangerous" and not "forbidden for any case". Every rule has some exceptions. It's always dangerous to modify arrays in place, that's why it needs double attention, very nice test coverage, and a lot of edge cases coverage, and a good programming team behind.

In this article there is a kind of "show off some syntax hacks", which IMHO opinion, some are hard to read, some are dangerous, especially for immature programmers, and some are "please read Ecma Script latest changes and don't be so cocky" :D

Thread Thread
leob profile image
leob

Haha agreed, especially the second paragraph ;)

The techniques in this article are okay-ish, but I'm not blown away if I may put it like that.