Re:Using ‘switch’ to match arbitrary logic instead of string...
by rhinoceraptor on Monday August 19, 2019 @12:46PM
(#20739340)
As others have said, this pattern is used for pattern matching when the language doesn't have specific constructs for it.
When I use it, I will extract it into a separate function, and just return the needed condition. That way you don't need to worry about case fall through.
function getCondition(param) {
switch (true) {
case ConditionOne(param): return 'condition one'
case ConditionTwo(param): return 'condition two'
case ConditionThree(param): return 'condition three'
}
}
When I use it, I will extract it into a separate function, and just return the needed condition. That way you don't need to worry about case fall through.