javascript - Is this a bad design pattern? -
In a recent discussion for junior JavaScript developers, I mentioned the following techniques to reduce laboriousness if / Or blocks where OR operand is used:
if (/^(cat_dog_horse)$/.test(animal)) {...}
instead of
if (animal == 'cat' || animal == 'dog' || beast == 'horse') {...}
I have never had any problems with this but a person suggested This was a poorly designed pattern, because without extension, I think that "trying to be very smart" and in doing so, you have introduced many new potential points (in reggax syntax) of the failures, and Code is made less expression / idiotic
Usually, in this way I am only going to work, you can also fight if your operands 'cat'
Being either dynamic or variable in some way Areas were going. As far as an array is started:
if (['cat', 'dog', 'horse']. IndexOf (animal)! = -1) {... }
Honestly, though, all this laugh is subjective, so there is no "right" answer that I can give you.
Usually at this time I present performance concerns, but you can actually get a quick solution here only because of the need to scan the input, depending on how fast Rizx Can be parsed.
Comments
Post a Comment