Last active
June 9, 2020 14:19
-
-
Save mdwheele/1f84a83e1d05bc6ed6a714910ea57e1d to your computer and use it in GitHub Desktop.
Don't filter like this
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AAAAAAAAAAAAHHHHHHHHHHHHHHHHHGGGGGGGGGGGGGG.
Yup, that's a code smell. Flag it.