Parse, Don't Validate – In a Language That Doesn't Want You To
cekrem.github.io
Parse, Don't Validate – In a Language That Doesn't Want You To
1–10 of 107 posts
Re: Parse, Don't Validate – In a Language That Doesn't Want You To
#2The reason I've not is - say there's an optional field. Currently we call that null, probably, and check each time if it's there or not. I could instead make a type, like User and UserWithPhoneNumber. Should we be making types for each combination of present/absent fields? That can't be right.
The classic answer is to move the logic inside the domain object, or have a helper function outside the object, so you aren't constantly checking for field presence/absence, but are instead writing the logic once and calling some code.
I'm not sure in practice types can help with this. But I'd love to be proven wrong.
Re: Parse, Don't Validate – In a Language That Doesn't Want You To
#3Re: Parse, Don't Validate – In a Language That Doesn't Want You To
#4This feels right, and I also have never done it (or had the guts to get others to do it). The reason I've not is - say there's an optional field. Currently we call that null, probably, and check each time if it's there or not. I could instead make a type, like User and UserWithPhoneNumber. Should we be making types for each combination of present/absent fields? That can't be right. The classic answer is to move the l…
class User{phone: ?PhoneNumber}
over class User{phone: ?string}.Re: Parse, Don't Validate – In a Language That Doesn't Want You To
#5This feels right, and I also have never done it (or had the guts to get others to do it). The reason I've not is - say there's an optional field. Currently we call that null, probably, and check each time if it's there or not. I could instead make a type, like User and UserWithPhoneNumber. Should we be making types for each combination of present/absent fields? That can't be right. The classic answer is to move the l…
The combinatorial explosion you're picturing only shows up if you make a separate type per combination of present fields, but you don't need to. An independent optional field stays one `T | null`. You only reach for distinct types when fields are correlated and present together because they represent a state, and then it's a discriminated union on a status field, which is N states, not 2^N.
Re: Parse, Don't Validate – In a Language That Doesn't Want You To
#6Re: Parse, Don't Validate – In a Language That Doesn't Want You To
#7Re: Parse, Don't Validate – In a Language That Doesn't Want You To
#8This feels right, and I also have never done it (or had the guts to get others to do it). The reason I've not is - say there's an optional field. Currently we call that null, probably, and check each time if it's there or not. I could instead make a type, like User and UserWithPhoneNumber. Should we be making types for each combination of present/absent fields? That can't be right. The classic answer is to move the l…
if a user with/without phone number are equally valid states to be then types won't help you much. I think it's more about writing class User{phone: ?PhoneNumber} over class User{phone: ?string}.
It's more about writing
struct User {phone: MaybePhoneNumber} // give or take, it's a monoid
over struct User {phone: Option}Re: Parse, Don't Validate – In a Language That Doesn't Want You To
#9Re: Parse, Don't Validate – In a Language That Doesn't Want You To
#10This one barely scrapes by at what feels like 30-40% "slop": "honestly", "the one thing", etc...
...but I did learn something about "Brand" types, and have personally tried to do more of "parse don't validate" in my own code.
Recently I did this similar trick for `exec( ValidExecutable(...) )` [python], where it required tagging/washing through a private function/variable to "get" the private bit.
All the scanners tend to light up when they see "exec" at all (eg: `exec( "pandoc" )` for PDF generation), but I needed to hard code a few "expected" pandoc locations so the imaginary hackers couldn't shadow "pandoc" on a path location they controlled.