DEV Community

Discussion on: 5 Programming Patterns I Like

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀 • Edited

Everything else looks fine but the nested turn ops.. not for me, clarity can go a long way over terse for the sake of others. Besides there are alternatives to nested conditionals. Boolean array .every or .some spring to mind.

// similar to x && y
const criteria = [
   user === 'logged-in',
   hasCardDetails
]
if ( criteria.every(isTrue => isTrue) ) {
   // do a thing if all are true
}
// similar to x || y
const criteria = [
   user === 'logged-in',
   hasCardDetails
]
if ( criteria.some(isTrue => isTrue) ) {
   // do a thing if some are true
}
Collapse
 
adamgerthel profile image
Adam Gerthel

Clever! Gonna remember this one

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀 • Edited

Your most welcome 😄, this is functional programming at it's finest. The only thing I don't like is the need to return the result by lambda. I haven't tried it but perhaps the Boolean constructor could sit where isTrue => isTrue is currently? I am afk so can't test.

Thread Thread
 
adamgerthel profile image
Adam Gerthel

I haven't tried it but perhaps the Boolean constructor could sit where isTrue => isTrue is currently?

This sentence went "swoosh" over my head.

I also had to google what a lambda was - had never heard that name for an anonymous function before (but I'm an autodidact which could explain it..) 😂

Could you elaborate in an ELI5 way? :D