Re:Ask HN: Do You Use Enum for Yes, No, Unset?
by varispeed on Friday June 17, 2022 @04:44PM
(#31784108)
You need a third state to know that user has not made a choice. If you don't want to make a choice for the user, for instance, you don't want to default to F or T, then third state tells exactly that user has to make a choice.
Re:Ask HN: Do You Use Enum for Yes, No, Unset?
by nescioquid on Friday June 17, 2022 @05:06PM
(#31784384)
Agreed. Sorry if I wasn't clear, but I consider that to fall into the "value is really nullable and you are using the presence of null to decide anything..." scenario, in which case, this is important to the domain, so explicitly model it so the next dev doesn't trip over the special meaning of null in this case.
Again, personal judgment and aesthetics, probably.
Re:Ask HN: Do You Use Enum for Yes, No, Unset?
by withinboredom on Friday June 17, 2022 @08:39PM
(#31786268)
If it isn’t selected, the browser literally won’t send anything for that field (undefined in js parlance). Your code/language of choice has to make a decision on what to do with that state. If you specify true/false, the page will reload with that state instead of “undefined.”
For some languages it is exactly as you describe, for lower level languages (as in closer to the raw HTTP protocol) like PHP, you have to make an explicit choice before rendering the response.
If you allow users to complete the form without choosing T/F on your field, what happens? If you fail or prevent the submission, then I would not write the schema to capture the value as a nullable field. If you do accept the form without the user selecting T/F, then you are defaulting to either T/F and the field should likewise not be nullable.
If the value is really nullable and you are using the presence of null to decide anything, then it seems reasonable to enumerate the state space explicitly with an enum so that the next dev riding by on a horse doesn't mistake your ternary boolean for a simple null.
You can make null work in any of those cases, it just seems like a headache and potential hazard, but could also just be personal aesthetics on my part.