3 ms·
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 dam
by chmod775 7y ago
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.
- ceejayoz 7y ago> 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.)
- chmod775 7y ago> (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.
- ceejayoz 7y agoI'm not sure why you'd think learning a little about widely-used JS libraries wouldn't make you "a more knowledgeable JavaScript developer".
- chmod775 7y agoHonestly? 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.
- isoskeles 7y ago> 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.
- chmod775 7y ago> 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".
- ceejayoz 7y agoIt has the same fundamental function. JS just has the concept of "false-ish", which you'll likely already know as a JS developer.
- chmod775 7y agoThe 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.
- dragonwriter 7y ago> 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
- irrational 7y ago>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."
- chmod775 7y agoThe 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.
- marmada 7y agoThere'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.