Just wondering, how come this code only checks the first invalid input, if they are all independent? Does that mean in this domain it’s not useful to find all of the invalid inputs at once?
Let's keep in mind that I was refactoring old code, I wouldn't design a validation system like this to start with. I kept it like this however because I didn't want to change any functionality during the refactor, and this code is in a request handler in a web service and the client can't handle a response with multiple errors anyway.
The use of the ternary operator for this seems possible, but in my opinion it's leaning in the direction of being cute for cute's sake rather than really being useful. At the very least I would argue it's not idiomatic js, and if there is a benefit to it at all, I don't think it's significant enough. In general I like the idea of using the idiomatic approach for a given language unless there is really quite a strong reason not to.
All that being said, if you and your team are comfortable with this code, I don't think using it is the end of the world.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Just wondering, how come this code only checks the first invalid input, if they are all independent? Does that mean in this domain it’s not useful to find all of the invalid inputs at once?
Let's keep in mind that I was refactoring old code, I wouldn't design a validation system like this to start with. I kept it like this however because I didn't want to change any functionality during the refactor, and this code is in a request handler in a web service and the client can't handle a response with multiple errors anyway.
No worries. In that case I think I’d prefer just (pseudocode):
I think it’s more straightforward.
Update:
The use of the ternary operator for this seems possible, but in my opinion it's leaning in the direction of being cute for cute's sake rather than really being useful. At the very least I would argue it's not idiomatic js, and if there is a benefit to it at all, I don't think it's significant enough. In general I like the idea of using the idiomatic approach for a given language unless there is really quite a strong reason not to.
All that being said, if you and your team are comfortable with this code, I don't think using it is the end of the world.