Live data from Hacker News

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

news.ycombinator.com

41–43 of 43 posts

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

#41
post #36
post #28

Earlier quoted context omitted.

And it's terrible because there's both `null` and `undefined` which have largely the same meaning but aren't equal. Depending on what you're interacting with, you may need to use one over the other or coerce. Especially common with developers from other languages who don't even know of the gotcha

I've never found a use for null where undefined wouldn't be simpler to use. Especially with the ?? Operator to set defaults along the way

This is a very curious and fun way to put it:

https://stackoverflow.com/a/57249968

In essence, `undefined` is non-existence. And `null` is existence with a non- (or "invalid", or "unknown") value.

Although using them like that depends on the needs of the application. If "invalid" or "unknown" was an expected user choice, I would still rather encode it as an enum better than a nullable boolean, as mentioned in my comment above.

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

#42

I work in a slightly different context but have dealt with a similar problem. I build Unity3D apps and I write a lot UI for the editor that affects how things are serialized. I've stopped serializing enums due to being burned too many times. When people: -Add entries not at the bottom -Rename entries -Remove entries -Rearrange entries So instead I made a kind of data-driven enum that you create through the editor. Ea…

It also means that there is no way to guarantee that you have covered all possible values for the enum, whereas if an enum value was removed in your older example, you would get a compile error.

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

#43
post #42

I work in a slightly different context but have dealt with a similar problem. I build Unity3D apps and I write a lot UI for the editor that affects how things are serialized. I've stopped serializing enums due to being burned too many times. When people: -Add entries not at the bottom -Rename entries -Remove entries -Rearrange entries So instead I made a kind of data-driven enum that you create through the editor. Ea…

It also means that there is no way to guarantee that you have covered all possible values for the enum, whereas if an enum value was removed in your older example, you would get a compile error.

Covered in what sense? As in tested? I can easily open the data driven enum amd push all values through some switching code if desired.

To alert the user about removed values I have code which checks key values against the definition and errors on deleted values. Using tokenized enum values is convenient for the programmer but again I want to discourage it as it often blocks the non-coding designers on my team when they need to extend a component to handle more app states.

Compiler token checking is nice but IMO data driven behavior is nicer. We dont avoid the use of strings and ints or any other kind of data because they don't have a compile time tokenized representation.

Post reply on HN