Live data from Hacker News

Ask HN: Do You Use Enum for Yes, No, Unset?

news.ycombinator.com

1–10 of 43 posts

Ask HN: Do You Use Enum for Yes, No, Unset?

#1
I've seen different arguments on this topic and thought I'd ask HN if you all have a standard for this.

Say you have a form that's filled out and saved to a DB. One required field is a dropdown with Yes and No. But the field begins as blank to show that a value hasn't been selected yet, ensuring that the User pays attention to the field.

Do you use a Nullable Bit (DB) and Nullable Bool (Code)? OR an Enum: Yes, No, Unset?

You wouldn't use a boolean to represent states like say New, Processing, Done. But do you consider NULL a separate "state"?

Thanks

Re: Ask HN: Do You Use Enum for Yes, No, Unset?

#3
The "ternary boolean" strikes me as a smell because it has to do with how you initialize your code/state and enforce pre- and post-conditions.

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.

Re: Ask HN: Do You Use Enum for Yes, No, Unset?

#6
There are two common "nulls" in this situation, the "unset" one and the "unknown" one. Unset would imply no one has ever set the value, unknown means someone has set the value to unknown which is sometimes an important distinction.

Javascript this is easy undefined can represent unset, and null can represent unknown. This is very useful for updating the state as well, undefined means do not change the value while null means update the value to unknown.

I have used code (enums) for yes/no/unknown that allow null value for unset as well in relational db's. If I had a choice I prefer JavaScripts data model of a boolean that can be undefined or null as well.

A nullable boolean in general has been better for a tristate than enums, but like I said many times I need a quadstate.

Re: Ask HN: Do You Use Enum for Yes, No, Unset?

#8
Yes/no/unspecified is a good match for something like `Option` or `bool?`. Their usage covers every case and nothing needs to be elucidated.

With that said, in a language that supports null and has no compile-time mechanism to detect potential null dereferencing (like modern C# for instance) I might use an enum.

Re: Ask HN: Do You Use Enum for Yes, No, Unset?

#9
For SQL there is usually an optimization where nullable columns are simply a bit set; same with boolean/bit columns. This does sound like a good use of a nullable bit.

You may want to distinguish things further in the future such as adding a new column and you can determine if it was unset/undefined by the user versus null because the user never saw it. In that case an enum makes sense.

Personally I think the logic maps directly to nullable bit.

Post reply on HN