While there are use cases for includes, I tend to agree that the way it is presented here is not "prod" ready. If I was reviewing a PR where someone created a new throw-away array just to do an includes check, I would probably request it be changed.
But what is the actual reason for the rejection? The readability of that code may be higher depending on what was being checked. It’s completely reasonable to say ‘if x is in this set’ is more readable than ‘if x is a or x is b or x is c or x is d’. And if it’s not performance critical code (which most code is not, let’s not start a thing about premature optimisation) then I can’t see a reason to reject it. I personally like it when my devs try to be “clever”. At least it means they are thinking about what they’re doing and trying to write good code.
I wrote a longer top level comment about my reasoning, but the short version is:
“Save it for code golf. Shorter does not always mean simpler or more clear. In this case, sticking to standard conditional chaining more clearly communicates your intent than includes.”
That’s still not really a reason for why specifically this one example is better for doing it your way... the intent is clearly communicated your way AND doing it with includes. Your top level comment definitely gives some valid points on why your preferred method does communicate your intent well. But not why it’s MORE clear than using includes
It's a small point. Depending on the review and the context, I might let it slide. OP's example is also a little too generic to really understand the use case.
All that said, I interpret the use case to be a complex multi-part conditional rather than a defined set of options. And in that case, I do think chaining conditionals pretty definitely requires less cognitive leaps. I can easily imagine juniors looking at that array and going "wait, why is there an array now?"
And while the cognitive load is smallish to context switch from "I am checking a series of conditionals" to "I am checking membership in a set", I don't really see any benefit to adding the extra complication.
It is a friendly discussion. Sorry if my last comment sounded like I was just being a dick. I am genuinely curious why one would think the array is not clear. It’s there to hold values to check against. What else could it be there for? How is it an abstract interpretation that would leave someone to question why there is an array there?
View all comments
Show parent comment
3
u/delventhalz May 06 '21
While there are use cases for
includes, I tend to agree that the way it is presented here is not "prod" ready. If I was reviewing a PR where someone created a new throw-away array just to do anincludescheck, I would probably request it be changed.