3 ms·
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 s
by cced 5y ago
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.
- planb 5y agoThat’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.
- Sayrus 5y agoEither 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.