News for hackers. Stuff that matters.
Re:You-Dont-Need-Lodash-Underscore by vfc1 on Friday August 09, 2019 @01:12PM (#20656313)
I don't understand the continuous backslash against such a useful tool like lodash, we add 4kb and have all the utility functions that we will ever need, what's wrong with that?

I feel that most people repeating this "you don't need lodash" meme are just saying it because they read it somewhere, and think that bashing on lodash makes them sound cool and looking like they know what they are talking about, much like it happened with jQuery.

There is way too much focus on micro-optimizations like handwriting/copy/pasting common utility functions each time to save 3.5kb in a bundle.

It's just one more of those many examples of engineering perfectionism gone counterproductive, which is, unfortunately, an attitude that is pervasive in this profession and that it rarely gets called out.

Re:You-Dont-Need-Lodash-Underscore by protonimitate on Friday August 09, 2019 @01:28PM (#20656478)
I think it mostly stems from the meme that "pure JS is best JS" and anyone who relies on "unnecessary" libs is somehow a worse dev than those who implement everything by hand, regardless of if its required or not.

For personal projects, self education, or fun - go ahead and implement the entire lodash lib by hand! But in a business setting a 3.5kb reduction in bundle size and 10 hours spend refactoring the lodash code out of your project is probably a waste of time and money.

I'll agree with the advice that you shouldn't add dependencies until you need them, but saying "you never need lodash" is bad advice IMO. Absolutism is a bad quality to harbor, especially when it's over trivial things.

I know jQuery fell out of favor with the JS community and now is treated like a sign of incompetence but - just as everything in programming holy wars - at the end of the day everything is a tool, and you need to pick the right tool for the job. If the job is "get an MVP up as quickly as possible" and lodash/jQuery/underscore helps you do that, why wouldn't you use it?

Re:You-Dont-Need-Lodash-Underscore by gmanley on Friday August 09, 2019 @01:35PM (#20656542)
Personally one of the biggest red flags for me is when a developer doesn't use libraries and writes everything themselves. That opensource project with many contributors, that has been around for years is probably going to be better than what you can write in couple passes. There are obviously times when you need to do it yourself or the library truly is badly written but those times are rare.

This gets a bit weird in the JS optimization world because it is true that each library and parts of the library you don't use are going to be extra useless bytes the user has to download. With tree shaking removing the parts of the library you don't use I think this becomes less of an issue.

Re:You-Dont-Need-Lodash-Underscore by delinka on Friday August 09, 2019 @02:09PM (#20656898)
The JS ecosystem has gotten so large that I can't possibly keep track of all the libraries that I could use to save myself trouble. This isn't helped by the fact that few tools & libs in that ecosystem state succinctly the problem they're attempting to solve.
Re:You-Dont-Need-Lodash-Underscore by zawerf on Friday August 09, 2019 @01:48PM (#20656682)
I think the current meme/trend is to drop as many dependencies as possible. Which is probably the overcorrection of the previous trend of using single utility function libraries like "leftpad".

I do agree with this specific instance though, that you should drop lodash.

Historically lodash/underscore was written for a time before a lot of syntax/standard libraries were added. The new javascript features obviously had lodash in hindsight so they are pretty good at making common things syntactically easier to do or just straight up taking the good functions and standardizing it (e.g., spread operators, arrow functions, Object.assign, Array.flat, Array.flatMap, etc, etc)

Re:You-Dont-Need-Lodash-Underscore by tlrobinson on Friday August 09, 2019 @02:00PM (#20656816)
I think all else being equal (e.x. assuming sufficient browser support or polyfilling), people should prefer using standard / built-in functions, many of which have been added to JavaScript since Underscore was popularized.

However, I don't necessarily agree with this approach of providing snippets for functions which aren't implemented natively. Underscore and Lodash likely have better implementations and much more testing. If you're concerned about bundle size, use tree shaking or individual function imports.

I think one exception to this is if you are writing a library and want to keep dependencies to an absolute minimum.

Re:You-Dont-Need-Lodash-Underscore by mcphage on Friday August 09, 2019 @03:03PM (#20657360)
> people should prefer using standard / built-in functions

I get that, but that's not what this list is: this list is re-implementing lodash / underscore using built-in functions. They're not a 1-for-1 replacement, they're complex implementations which may or may not be correct or efficient. If their solution for replacing _.chunk(...) was some built-in Array.chunk(...) or something, that would be fine. But instead, it's this:

> const chunk = (input, size) => {

> return input.reduce((arr, item, idx) => {

> return idx % size === 0

> ? [...arr, [item]]

> : [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];

> }, []);

> };

That's definitely worse than using lodash.

Re:You-Dont-Need-Lodash-Underscore by tlrobinson on Friday August 09, 2019 @03:39PM (#20657642)
That's exactly what I said in my second paragraph...
Re:You-Dont-Need-Lodash-Underscore by mcphage on Friday August 09, 2019 @04:59PM (#20658218)
Ah, you did. Sorry about that, you’ve caught me running my mouth off before I finish reading.
Re:You-Dont-Need-Lodash-Underscore by chmod775 on Friday August 09, 2019 @01:45PM (#20656654)
Case in point: "_.compact".

When I see stuff like

    const cleanInput = _.compact(input);
I have no idea what the fuck just happened. I have to open the damn documentation of lodash (or whatever else kind of library the author chose to use) to figure out what's going on.

Now if that was

    const cleanInput = input.filter(elem => !!elem);
I could've gotten on with my day and saved some time. This does not need any documentation or comment.

This might seem like a small issue to you, but damn, I spend a lot of time debugging random projects on GitHub, and the inability of the JavaScript community to write readable code really trips me up every time. Instead of just writing 2-3 lines other people can parse in a second or two, they spend 5 minutes looking up the relevant library function (which doesn't do what they expect half the time) to produce one shitty line of code other people will also spend 5 minutes on trying to decipher it.

Re:You-Dont-Need-Lodash-Underscore by irrational on Friday August 09, 2019 @02:03PM (#20656842)
>This does not need any documentation or comment.

I honestly have no more clue what const cleanInput = input.filter(elem => !!elem); means than const cleanInput = _.compact(input); without going to the documentation.

What you meant to say is "This does not need any documentation or comment for myself, because I have already memorized what all the parts of this line of code mean, whereas I have not yet memorized what _.compact means."

Re:You-Dont-Need-Lodash-Underscore by marmada on Friday August 09, 2019 @07:08PM (#20658936)
There's nothing to memorize here, or there shouldn't be, because "filter" is a standard array function in JS that I imagine most JS developers have encountered.

Knowing how "filter" works is valuable because now you can use that knowledge in the future on any project that uses JS, unlike "_.compact" which is only applicable in projects that use lodash. You can be relatively confident that you can write "filter(elem => <some function>)" and that other developers will know what "filter" is doing.

The second part is more tricky, but still useful.

The !! operator is just two "!" operators. That's pretty normal too. The first "!" converts an object/number/whatever into the opposite of its boolean value. The second "!" negates the first boolean, thus turning the opposite of the boolean value into the object's original boolean value.

Even if figuring this out on your own is slightly more difficult, it gives you valuable insight into the language (JS is very dynamically-typed and the not operator can cast objects to booleans), which will help you write better/safer/more concise/clearer code in the future.

Re:You-Dont-Need-Lodash-Underscore by chmod775 on Friday August 09, 2019 @02:09PM (#20656893)
The difference is that one thing uses JavaScript fundamentals that (except for the arrow function shorthand) have been part of the language since JavaScript 1.6 and the other is just some random library.
Re:You-Dont-Need-Lodash-Underscore by ceejayoz on Friday August 09, 2019 @01:47PM (#20656672)
> This does not need any documentation or comment.

Sure it does. You've just already read the documentation for filter.

(And frankly, I just had to look up what !! does, despite years of writing JS code.)

Re:You-Dont-Need-Lodash-Underscore by chmod775 on Friday August 09, 2019 @02:16PM (#20656965)
> (And frankly, I just had to look up what !! does, despite years of writing JS code.)

It's literally just 2 NOT operators chained together. I won't fault you for never having seen it, but it is curious considering how common it is.

In any case. Looking that up just made you a more knowledgeable JavaScript developer, while looking up some random lodash function won't buy you much at all.

Re:You-Dont-Need-Lodash-Underscore by ceejayoz on Friday August 09, 2019 @02:41PM (#20657180)
I'm not sure why you'd think learning a little about widely-used JS libraries wouldn't make you "a more knowledgeable JavaScript developer".
Re:You-Dont-Need-Lodash-Underscore by chmod775 on Friday August 09, 2019 @02:52PM (#20657259)
Honestly? It probably does. Much less so than learning the actual language built-ins first, but it does. I'm just biased because I consider them more of a pain than anything because of my personal experience with them.

Memorizing multiplication up to 100 will also make you faster at calculating stuff in your head, but I couldn't ever be bothered to do that either.

Re:You-Dont-Need-Lodash-Underscore by isoskeles on Friday August 09, 2019 @01:49PM (#20656690)
> I have to open the damn documentation

I don't know if you understand how lucky you are to be able to do this in the first place.

Also, the "compact" method, I don't know if this is the reason it gets its name in lodash etc., exists in the Ruby language and does the same thing.

Re:You-Dont-Need-Lodash-Underscore by chmod775 on Friday August 09, 2019 @01:52PM (#20656733)
> exists in the Ruby language and does the same thing.

You just got lodash'd!

Because the ruby method DOESN'T do the same thing. And this is precisely why you have to consult the documentation every single time you see something weird like that.

The difference is that the ruby method doesn't remove stuff like "0" and "false", just "nil".

Re:You-Dont-Need-Lodash-Underscore by isoskeles on Friday August 09, 2019 @01:59PM (#20656803)
Haha, would you look at that.

Still, I prefer having documentation over the possible scenario where I have a few undocumented functions created by other developers for whatever reason. Maybe multiple functions do the same thing. Not to say this is the only alternative, but it is the more common alternative I've seen years ago, and it's a certain type of hell.

Re:You-Dont-Need-Lodash-Underscore by ceejayoz on Friday August 09, 2019 @01:54PM (#20656744)
It has the same fundamental function.

JS just has the concept of "false-ish", which you'll likely already know as a JS developer.

Re:You-Dont-Need-Lodash-Underscore by dragonwriter on Friday August 09, 2019 @02:30PM (#20657091)
> It has the same fundamental function.

No, it doesn't.

> JS just has the concept of "false-ish", which you'll likely already know as a JS developer.

Ruby has a concept of false-ish (which is narrower than JS’s), but Ruby’s “compact” isn't about false-ish-ness, Filtering out false-ish values is .select(&:itself), not .compact

Re:You-Dont-Need-Lodash-Underscore by ceejayoz on Friday August 09, 2019 @02:55PM (#20657289)
Maybe my feelings here are colored by PHP being my primary lang, but I read both functions purpose as "remove the empty values from this array" with a language-level difference on what "empty" is defined as. PHP's "empty" includes both nulls and false-y values.

Whether you want to call it the "same" function or "similar" function, lodash's excellent docs reveal its functionality quite handily if someone's confused by it.

Re:You-Dont-Need-Lodash-Underscore by chmod775 on Friday August 09, 2019 @02:27PM (#20657065)
The ruby method removes "nil", not "false". The ruby method has absolutely nothing to do with false whatsoever, while lodash makes explicit reference to it. The ruby method is intended to just remove holes in arrays, not remove values. JavaScript has the concept of holes in arrays, but that's not what the lodash function is about.

They're 2 different functions/methods with 2 different ideas behind them. They're absolutely not each other's equivalent in their respective language.

Re:You-Dont-Need-Lodash-Underscore by lhorie on Friday August 09, 2019 @01:49PM (#20656695)
My job involves overlooking hundreds of internal codebases and I see people doing `import {range} from 'lodash'` every once in a while. One offending line and tada: 70k+ bundle size for no good reason...

I'd say that any argument for/against lodash can go both ways: if one argues that it is a micro-optimization to handwrite `range`, another person could argue that it's also a lazy devexp micro-optimization to use lodash. `range`, for example, can be implemented with a one liner in like 5 mins and tested thoroughly with a couple of asserts.

Maybe one could argue that some of the more compositional stuff is not so trivial to write (e.g. cond and friends), but if one's using those over normal small procedural functions, who's really engaging in engineering perfectionism gone counterproductive?

Re:You-Dont-Need-Lodash-Underscore by wnevets on Friday August 09, 2019 @01:38PM (#20656571)
>I feel that most people repeating this "you don't need lodash" meme are just saying it because they read it somewhere, and think that bashing on lodash makes them sound cool and looking like they know what they are talking about, much like it happened with jQuery.

I feel like this happens for a lot of topics in this industry, positively and negatively. e.g TypeScript has been on the receiving end for both.

Re:You-Dont-Need-Lodash-Underscore by thecleaner on Friday August 09, 2019 @01:25PM (#20656441)
Out of curiosity is jQuery really that bad ? I am not exactly sure why I would not want to use jQuery and prefer SPAs. What is actually missing in jQuery lodash handlebars combo ?
Re:You-Dont-Need-Lodash-Underscore by bobthepanda on Friday August 09, 2019 @01:42PM (#20656619)
All your template-based logic is in one place. Rather than listen to events and then executing actions using a mishmash of selector-fu, you can have a self-contained template that doesn’t care about other parts of your page. It hides a layer of nitty-gritty from the developer.
Re:You-Dont-Need-Lodash-Underscore by dymk on Friday August 09, 2019 @01:28PM (#20656479)
Managing anything more complicated than only a slight enhanced page quickly becomes a big ball of mud when you use jQuery and handebars (fancy string interpolation) for state management and UI updates.
Re:You-Dont-Need-Lodash-Underscore by ChrisCinelli on Friday August 09, 2019 @07:40PM (#20659109)
8 years ago I was using jQuery and good self contain JS objects. Each of them had responsibility over specific part of the DOM. The beauty of that is that in that why you can keep thing simple and updates already well optimized by default.

I understand that by default with JS you can write code that in one place touch every part of the page.

But no framework or language protect you from bad code practices and bad abstractions. They may avoid it at a specific abstraction level but unless you are disciplined and you know what you are doing, you are still screwed beyond that.

Component libraries like React did not invent encapsulation, abstractions, separation of concerns, loose coupling and high cohesion, they just enforce them at their level of abstraction but let you paying the "unoptimized" recalculate all by default.

In fact I saw a bunch of code written in React that are big balls of mud communicating together without a good abstraction logic.

Redux, in the React ecosystem, is pretty good at keep the state in one place but pretty bad to hide implementation details to those component that should not be concerned with them... result? Refactoring a reducer can results in refactoring a lot of smart components. And the verbosity of using Redux has always been off the chart.

The best part of React documentation is here:https://reactjs.org/docs/thinking-in-react.html - Especially in steps 3 and 4. Learn that and use it with the framework of choice or just vanillaJS... You do not use that and you will be in trouble with React or any other framework.

Re:You-Dont-Need-Lodash-Underscore by thecleaner on Friday August 09, 2019 @01:41PM (#20656601)
I see. What do you use and why ?
Re:You-Dont-Need-Lodash-Underscore by dymk on Friday August 09, 2019 @01:58PM (#20656792)
React+Redux. I can focus just on what the structure of the UI should be and not the nitty-gritty of actually updating it (React handles that). State management through Redux so I can consolidate where application state is held, and testing it is straightforward and a feature of the library.
Re:You-Dont-Need-Lodash-Underscore by _the_inflator on Friday August 09, 2019 @01:41PM (#20656608)
I take these "You don't bla" repos with a grain of salt. JavaScript evolves and if there are methods that are now (almost) natively covered by the language why not use them? Also, if you only need a few methods it makes sense to spare the 4kb and simply rely on a native implementation. Such collections then come in handy.