-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Update README.md #2430
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
base: master
Are you sure you want to change the base?
Update README.md #2430
Conversation
chety
commented
May 24, 2021
- Enhance method implementation by returning in a single line rather than using if and else.
- Add support for the default parameter value.
|
||
//best | ||
inbox.filter(({ subject, author } ) => { | ||
return subject === 'Mockingbird' && author === 'Harper Lee'; | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't actually best - our linter config will warn on it. it'd be:
//best | |
inbox.filter(({ subject, author } ) => { | |
return subject === 'Mockingbird' && author === 'Harper Lee'; | |
}); | |
//best | |
inbox.filter(({ subject, author }) => subject === 'Mockingbird' && author === 'Harper Lee'); |
however, then we've defeated the whole point of this section, which is showing patterns around explicit return
in callbacks.
so, i think this is best reverted.
// best | ||
function isNullOrUndefined(obj){ | ||
// '==' handles both undefined and null | ||
return obj == null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this shouldn't ever be a function, it should just be inline.
function getDefaultValueIfNotPresent(obj,defaultValue){ | ||
return isNullOrUndefined(obj) ? defaultValue : obj; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function getDefaultValueIfNotPresent(obj,defaultValue){ | |
return isNullOrUndefined(obj) ? defaultValue : obj; | |
} | |
function getDefaultValueIfNotPresent(obj,defaultValue) { | |
return isNullOrUndefined(obj) ? defaultValue : obj; | |
} |
this is entirely unnecessary, though, since ??
exists - you can just do obj ?? defaultValue
, without any need for a function.
|
||
// best | ||
[1, 2, 3].map((x) => x * (x + 1)); | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is fine, but it may not actually be better - the "y" might add clarity, depending on what the code is doing.
function handleThings(opts = getDefaultValueIfNotPresent(opts,{})){ | ||
// ... | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't think this adds anything over the existing example of function handleThings(opts = {}) {
.
function handleThings(opts = getDefaultValueIfNotPresent(opts,{})){ | |
// ... | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you explicitly pass null
to handleThings
function then opts
will be null rather than the default {}
value. Therefore I think we should also handle this case.