Skip to content

Avoid lodash#11

Open
ledamint wants to merge 11 commits intomasterfrom
avoid-lodash
Open

Avoid lodash#11
ledamint wants to merge 11 commits intomasterfrom
avoid-lodash

Conversation

@ledamint
Copy link
Contributor

No description provided.

```
## External libraries

### Prefer native js vs lodash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a good section, i like it. my common feedback - please add few words regarding to polyfills. Otherwise it looks unsafely to swapping to native methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one more - is adding a few words when lodash is preferred, say reduce an Object, or find via object, is a shorthand's we prefer.

_.reduce({foo: 'FOO', bar: 'BAR'}, callback, {});
_.find( [{baz: 10}, {baz: 30}, {baz: 50}], {foo: 30});

Copy link
Contributor Author

@ledamint ledamint Jan 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we will leave only bad cases to not divide all lodash functionality to good or bad? I described popular and often used methods. In cases above, we can use Object.(keys|values|entries) and after use array methods.

Copy link
Contributor

@DmitryKorlas DmitryKorlas Jan 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we have to provide a list of all bad technics we have to replace to native alternatives. Regarding to using two methods vs one: using two methods vs one is not a good choice. We have to make our code simple and do not introduce extra boilerplate code. Lodash is created for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a rare case where you need reduce to object and I think it's nice to directly show where is a plain array or where is an object which you need to use as an array. You use one short method with particular cases and your code remains strict, native and without external modules except using external functionality which eats any type

Copy link
Contributor

@DmitryKorlas DmitryKorlas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good section, i like it. my common feedback - please add few words regarding to polyfills and examples when lodash is preferred.

@DmitryStolbov
Copy link
Contributor

Need more examples for Object, for instance:

// BAD
_.isEmpty(value)
// GOOD
!Object.keys(value).length

mocha: true
},
extends: 'eslint:recommended',
extends: ['plugin:you-dont-need-lodash-underscore/all', 'eslint:recommended'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disagree. This one is too strict and not cross browser. I don't think we have totally forbid lodash. A lot of cases when lodash functionality is better choice vs native build-ins. I guess we have to prefer native methods when possible, but do not dictate using only native methods by introducing this rule into guide.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not all at all. It is just which are easy to replace by native implementation https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore/blob/master/lib/rules/rules.json

// BAD
_.uniq([1, 2, 3, 3, 2]);
// GOOD
[...new Set([1, 2, 3, 3, 2])];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one does not looks better vs lodash variant. _.uniq is more clear and shortener. Also, Set has cross browser limitations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Native and one line, easy to understand or to google purpose if not. I suggest it's a nice choice, IE should die, thinking about it is an atavism in 2k19. It will be attention about polyfills

// BAD
_.includes([1, 2, 3], 1);
// GOOD
[1, 2, 3].includes(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.prototype.includes is not a cross browser and still not supported in IE11. Native variant is only preferred when polyfill is included. Otherwise it dangerous and we should not recommend it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a attention about polyfills


> Reason: lodash is a great library which contains a lot of useful functions but the modern js has the short identical part of this functionality so some lodash functions are excess alternative.

> Don't forget to check necessary browsers compatibility and turn on polyfills if it's needed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good note, I like it. I guess it would be better to clarify which variants is dangerous and add a link to MDN for every such method. We have to notify a users of this document.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to not create a compatibility map, all developers understand about using different functionality and should think about each method itself depending which browser support is needed for an application

Copy link
Contributor

@DmitryKorlas DmitryKorlas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the feedback in a comments

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

Successfully merging this pull request may close these issues.

3 participants