Skip to content

Instantly share code, notes, and snippets.

@mdwheele
Last active June 9, 2020 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdwheele/1f84a83e1d05bc6ed6a714910ea57e1d to your computer and use it in GitHub Desktop.
Save mdwheele/1f84a83e1d05bc6ed6a714910ea57e1d to your computer and use it in GitHub Desktop.
Don't filter like this
const produce = ['Apple', 'Potato', 'Banana', 'Cucumber']
// Using a temporary value to build up our result.
const fruits = []
// Bad: Using filter that has a side-effect (non-pure function).
produce.filter(p => {
if (['Apple', 'Banana'].includes(p)) {
fruits.push(p)
}
})
// Voila
console.log(fruits)
const produce = ['Apple', 'Potato', 'Banana', 'Cucumber']
// Better: filter the collection, return matches to build up fruits.
const fruits = produce.filter(p => {
if (['Apple', 'Banana'].includes(p)) {
return p
}
})
// Voila
console.log(fruits)
@mcamiano
Copy link

mcamiano commented Jun 9, 2020

AAAAAAAAAAAAHHHHHHHHHHHHHHHHHGGGGGGGGGGGGGG.

Yup, that's a code smell. Flag it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment