For all of the people encountering this use of includes for the first time: this is a neat trick! Keep it in your back pocket. I think it is particularly useful when you have a static set of options you need to check something against. Like this example:
const FRUIT = ['apple', 'banana', 'orange'];
function isFruit(item) {
return FRUIT.includes(item);
}
However, the heading seems to imply that this is a good general purpose replacement for checking multiple conditions, and I would caution against that impression.
Beware the allure of shorter code. Shorter code is not necessarily simpler or more readable. In OP's code snippet, two new pieces of syntax (the array and it's method) are introduced. More different syntax means more complexity.
Perhaps more importantly, you are stretching the new syntax a bit outside of it's typical use. The "semantic" goal of the code is to check multiple conditions. The purpose of || is to join multiple conditions. Totally aligned. Totally expected.
What is the purpose of includes? To check if an array includes a value. What array? Our code doesn't have an array. Okay, so we make an array so we can forceincludes to work for us. Think about the complexity of that for a moment. We are making a throw-away array for purely mechanical reasons. A good goal when programming is to try to avoid code that is purely mechanical. Instead try to make your code more "declarative". In other words, write code that just tells you what it does instead of getting into mechanical details. From that perspective, the || actually beats includes in this case.
For what it's worth, I typically solve this sort of complex multi-conditional by making the code longer. No new syntax, I just add whitespace.
const isOneOrTwo = value === 1
|| value === 'one'
|| value === 2
|| value === 'two;
if (isOneOrTwo) {
// Execute some code
}
I like this approach for a few reasons. It puts each condition on its own line. Like a nice bullet-pointed list. It also allows me to give the condition a meaningful name, which hopefully clarifies its purpose. Finally, it quarantines the conditional off from the rest of the code. If someone wants to understand the conditional, they can look at it in isolation. If someone wants to understand the if-block, they don't have to worry about the details of the condition.
TLDR: Shorter is not the same as simpler. Stick to what is expected when possible. This is a neat trick, but chaining || and && is probably better most of the time.
If it is a dynamic set of options, then don't you already have an array you are working with?
The post is about checking "multiple conditions". If we are actually talking about checking whether or not an array includes an item, then yes, includes is absolutely the right choice.
Not necessarily. A beginner might do something like have different functions with different sets of possible conditions. It isn't immediately obvious that you can translate logical conditions into an array in this way. For me that is what is useful about this post.
12
u/delventhalz May 06 '21 edited May 06 '21
What a contentious thread!
For all of the people encountering this use of includes for the first time: this is a neat trick! Keep it in your back pocket. I think it is particularly useful when you have a static set of options you need to check something against. Like this example:
However, the heading seems to imply that this is a good general purpose replacement for checking multiple conditions, and I would caution against that impression.
Beware the allure of shorter code. Shorter code is not necessarily simpler or more readable. In OP's code snippet, two new pieces of syntax (the array and it's method) are introduced. More different syntax means more complexity.
Perhaps more importantly, you are stretching the new syntax a bit outside of it's typical use. The "semantic" goal of the code is to check multiple conditions. The purpose of
||is to join multiple conditions. Totally aligned. Totally expected.What is the purpose of
includes? To check if an array includes a value. What array? Our code doesn't have an array. Okay, so we make an array so we can forceincludesto work for us. Think about the complexity of that for a moment. We are making a throw-away array for purely mechanical reasons. A good goal when programming is to try to avoid code that is purely mechanical. Instead try to make your code more "declarative". In other words, write code that just tells you what it does instead of getting into mechanical details. From that perspective, the||actually beatsincludesin this case.For what it's worth, I typically solve this sort of complex multi-conditional by making the code longer. No new syntax, I just add whitespace.
I like this approach for a few reasons. It puts each condition on its own line. Like a nice bullet-pointed list. It also allows me to give the condition a meaningful name, which hopefully clarifies its purpose. Finally, it quarantines the conditional off from the rest of the code. If someone wants to understand the conditional, they can look at it in isolation. If someone wants to understand the if-block, they don't have to worry about the details of the condition.
TLDR: Shorter is not the same as simpler. Stick to what is expected when possible. This is a neat trick, but chaining
||and&&is probably better most of the time.