2 ms·
>Now I have a single, snappy slogan that encapsulates what type-driven design means to me, and better yet, it’s only three words long: >Parse, don’t validate.
by kgeist 2y ago
>Now I have a single, snappy slogan that encapsulates what type-driven design means to me, and better yet, it’s only three words long:
>Parse, don’t validate.
For me the slogan is rather "always validate only in the single constructor" (or constructor function, doesn't matter). That way, you cannot have invalid objects at all, and there's always a single source of truth. If you want to modify the object, implement it via constructing a new state by calling the same constructor again.
- epolanski 2y agoNot really the same thing. The point is that validation alone is then lost as information later on. E.g. validating an int to be positive has limited benefits if you don't parse it to be a positive int, because there's no such information at the type level later on, same could apply to a non empty array/list where following consumers would then need to check again if the list is really non empty. This kind of information cannot always be encoded in objects or constructors.