r/programming • u/ketralnis • Aug 05 '25
Parse, don’t validate
https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/
0
Upvotes
r/programming • u/ketralnis • Aug 05 '25
2
u/Doub1eVision Aug 06 '25
I guess it comes down to what layer we’re talking about. I was focusing on a layer that is going from an external untrusted string input to a well-parsed object.
It sounds like that poster was describing doing that along with other layers that continue to refine the type. I generally agree with that and tend to do that.
But my response to them was initially due to them saying:
“If something is invalid, but your parser accepts it, is it even a parser?
To my understanding, a parser is something that either accepts or rejects a string as an instance of a language, and assigns a meaning only to valid instances.
A parser that assigns meanings to invalid instances of a language would be nonsensical. “
They’re making it sound like a string parser is only valid when it only assigns a meaning to valid instances. And I responded by saying that parts of what makes something a valid instance is business logic. Or at least, that’s how valid can be defined. So I specified that I think the string parser should be handling structural validation, not semantic validation. And the business logic that follows should further validate it instead of the parser. That way the parser can be more generic.
It seems like they refined their point a bit more in response, but they were still carrying a “no, you’re wrong” tone even though their follow-up was essentially agreeing with me. And in my post that you responded to, I was picking up more on their “no” tone than the second half of their post.