4 ms·
Help me out if I'm missing something important, but this doesn't really seem to add much except for the conceptual overhead of another control structure. The e
by couchand 11y ago
Help me out if I'm missing something important, but this doesn't really seem to add much except for the conceptual overhead of another control structure.
The examples given in the article are as clear or clearer without the do syntax. Compare:
[1, -1, -0.5].map(x => do { if (x > 0) { x * 10 } else { -x * 10 } })
to
[1, -1, -0.5].map(x => { if (x > 0) { return x * 10 } else { return -x * 10 } })
as well as
var data = do {
const data = pullSomeData()
doSomethingElse() // sideEffect
data
}
versus
const data = pullSomeData() // or `var` if you wish
doSomethingElse() // sideEffect
Perhaps a more significant example is warranted here. My suspicion is that if you get code complicated enough to justify a `do` block, it's complicated enough to justify a function that can be independently tested.
- acjohnson55 11y agoI agree that the examples aren't particularly motivating. But I think it really shines in JSX blocks (or other simple branches in literal data structure definitions). It's also super helpful when needing to initialize several variables based on branching on several other values. Having expression-if combined with array unpacking assignment makes this a breeze.