Skip to content
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

Lodash/Underscore are partially still simpler and easier to read #333

Open
rkrisztian opened this issue Dec 13, 2021 · 0 comments
Open

Lodash/Underscore are partially still simpler and easier to read #333

rkrisztian opened this issue Dec 13, 2021 · 0 comments

Comments

@rkrisztian
Copy link

rkrisztian commented Dec 13, 2021

Sure, some of the examples given there do a very good job at showcasing how ES evolved and brought us fine replacements for what we could only do nicely with these libraries. But some native alternatives are plain outright worse. Let's what I mean.

  1. _.chunk, _.pull: the native alternative means reinventing the wheel, and such actions in general are often sources of errors.
  2. _.difference, _.intersection are easier to read than most implementations using reduce, which is like the lowest level of all "functional loops", thus it's great for implementation details, bad for when that's the last thing we want to see (in a higher-level code).
  3. _.dropRight, _.takeRight, _.first are easier to read than slice. In fact slice tries to be too smart, often to the sacrifice of readability. I have been avoiding slice because it makes me think, it's like a puzzle. Sure if I use and see slice for thousands of times, my familiarity bias should be gone, especially that languages like Groovy also use negative indexes (list[-2]), but at the end of the day it's still more complicated than just using simple English words. There are popular keywords for shift/unshift/push/pop operations, some of which we have in ES already, but others just need to go there already, because slice wants to be too generic, like reduce. For that reason I'd rather take _.initial over .slice(0, -2), provided we all understand well enough what initial means, otherwise we of course need a better function name.

(I didn't check but the Array-related sections, but I think I've seen enough.)

=> So what we need is the best of both worlds:

  1. Libraries updated to support ES6 (and later as newer standards gain more spread). E.g. we can reimplement Lodash's _.dropRight with the use of Array.slice instead of whatever else we had there before to give nice names to native things. And that's what already happened:

https://github.com/lodash/lodash/blob/2f79053d7bc7c9c9561a30dda202b3dcd2b72b90/dropRight.js
vs.
https://github.com/lodash/lodash/blob/2900cfd288f72671c335021fa3220818445f9123/dropRight.js

  1. The good parts of Lodash/Underscore/etc. should be kept and still used. By no means we should say these libraries are completely useless now. Some parts still are.

  2. And if you really need the new standard but you have to support older browsers: then we got shims. But this should be used for the right reasons, like not missing out on updated libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant