4 ms·
> Now you just get to guess whether, for every class, there is actually a ValidClass that you were supposed to be using, instead of Class. The type system stop
by noelwelsh 2y ago
> Now you just get to guess whether, for every class, there is actually a ValidClass that you were supposed to be using, instead of Class.
The type system stops this. You can't pass, e.g. a User to authenticate because it only accepts a ValidUser.
> only make classes for valid objects
Using primitives (String, Int, etc.) for unvalidated input, and custom classes for validated input is fine in many cases. However, sometimes you need to represent data that is in the process of being validated (e.g. when validation takes time, like waiting for a user to validate their email address) and then these intermediate classes arrive.
- szundi 2y agoBut your colleage just implemented it with User - because he could
- noelwelsh 2y agoNo tool can fully protect against user incompetence (pun intended). You can have the fanciest type system in the world and still implement everything as a String, for example.
- fiddlerwoaroof 2y agoI think, for most cases with partial validation, you should parse the fields first into valid types for those fields and store them in a hashmap or something. When all the fields have been collected, parse the hashmap into your aggregate type.