Created
January 8, 2021 11:36
-
-
Save galabra/57bea6bc88dbb48696ce52f9e5c2e1bc to your computer and use it in GitHub Desktop.
These three versions of equivalent code demonstrates how code should be self-explainable
This file contains 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
dataArray.reduce((a, b) => [...a, | |
...(b.data && b.data.isImportant && b.data.value ? [b.data.value] : []) | |
], []); | |
/** | |
* Thwe horrific JavaScript snippet above is equivalent to the following | |
*/ | |
dataArray | |
.filter((x) => x.data && x.data.isImportant && x.data.value) | |
.map((x) => x.data.value); | |
/** | |
* And if you really insist on a single iteration, meaningful names | |
* improve readability significantly | |
*/ | |
dataArray.reduce((result, element) => { | |
if (element.data) { | |
const { isImportant, value } = element.data; | |
if (isImportant && value) return [...result, value]; | |
} | |
return result; | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment