DEV Community

Discussion on: Let's talk about Lodash

Collapse
 
lexlohr profile image
Alex Lohr

I found that helper tools like lodash, underscore, etc. tend to lend themselves to help the developer outsource complexity instead of actually understanding the underlying issue enough to reduce the complexity to a point where you get an elegant solution. While that may not always be possible, I find that not using lodash often results in better code. But if you use it, be sure to use it whenever it makes sense.

Collapse
 
bulletinmybeard profile image
Robin Schulz

I've been using Lodash and Underscore since their existence and have made use of them extensively! However, this changed half a year ago when I started working on a lightweight Microservice component that I build with Node.js, where every single byte counts.

Now that I got rid of Lodash/Underscore, I basically recreated the helper functions I was using; it's even more fun to use them as I now precisely know what they're doing and with what data types I'm dealing with and ultimately no longer need to be prepared for all eventualities and types.

P.S. I kind of agree with @lexlohr . Even for me and 20+ years of experience, I often didn't even try to understand how some of the helper functions work; I just used them and got instant results, eez.

I can definitely recommend using one of the two libraries when starting a new project. As soon as you know which helper functions you ultimately need, you can start replacing the library helper functions with your logic using Lodash its .mixin() feature to extend core helper functions with custom ones. In this case, you want to overwrite core functions with your logic: dustinpfister.github.io/2018/01/31.... If this is done, you can replace the Lodash helper functions with your own helper functions.

Cheers

Collapse
 
eldarshamukhamedov profile image
Eldar Shamukhamedov

I've found the opposite. Folks write custom implementations of deep merge, intersection, is plain object, etc. and miss edge cases. These helpers then end up being used all over the place, which makes it very hard to fix any issues.

For sure use native JS built-ins over Lodash, but if you're considering writing your own version of one of the more complex Lodash helpers, at least go read their source code first.

Collapse
 
janpauldahlke profile image
jan paul

y the deepcopy stuff is still very viable

Collapse
 
lexlohr profile image
Alex Lohr

That's what I mean with unnecessary complexity. There are less problems that strictly require these functions than one would think.

Also, things like deep cloning also have issues in lodash and other tools, see github.com/atk/object-clone-propos...

So using these tools is not a silver bullet. As I said, if you use them, make it count, but try to solve the underlying issue before you grab these tools.

Collapse
 
merri profile image
Vesa Piittinen • Edited

I dislike lodash, ramda etc. as JavaScript has become quite capable in expressing meaning very well. Reading code that suddenly uses utility functions makes it much slower to understand the code as you need to look up what those functions actually do. Everybody seems to like a different library and if project has not maintained discipline, well, life becomes troublesome for a maintainer.

Of course reading specs for just one function isn't a problem, but it seems like once somebody starts to think of a problem through utility functions they sure seem to make sure to use as many different utility functions as they can in one go. They have fun learning the functions, but as a reader the functions are more of a pain - and still all too often you can refactor the code to shorter native JS while not having undesired side effects (tests are nice).

Collapse
 
lexlohr profile image
Alex Lohr

That's another good point. While some of these helpers are pretty obvious in the functionality (though not in the implementation; you'll end up with a lot of unused edge case handling, take isEmpty for example), many of them require a lot of documentation and are quite difficult to read.