News for hackers. Stuff that matters.
Re:Using the switch(true) pattern in JavaScript by cced on Monday April 12, 2021 @11:07AM (#26780212)
I think the issue here though, is that for certain cases such as form field validation, you want all issues to be returned at once. Using the switch method or similar message packages allow you to inform your users that have N issues with a page in 1 request as opposed to N.
Re:Using the switch(true) pattern in JavaScript by planb on Monday April 12, 2021 @11:14AM (#26780291)
That’s not how throw works. And that’s exactly why the switch(true) pattern should not be used: novice users not fully knowledgeable about every specification of a language should not fail to understand such a basic piece of code.

There’s not even a line difference to using if statements correctly (as others in the comments have demonstrated) The one thing an if statement does is checking if something is ‚true‘. I don’t understand why anyone would use a ‚switch‘ here apart from showing how clever they are.

Re:Using the switch(true) pattern in JavaScript by Sayrus on Monday April 12, 2021 @11:15AM (#26780299)
Either you will break, thus exiting the switch-case block or you will fall-through. The fall-through behavior won't do what you want:

> the script will run from the case where the criterion is met and will run the cases after that regardless if a criterion was met.

Using the switch method won't allow you to return several errors while the simple ifs method described earlier could accumulate the errors and return later. The switch method is elegant though.