3 ms·
Strong disagree. enums + unions = ADTs. Without enums you have to use either string or number. The problem with using strings is that nothing else uses strings
by zarzavat 2y ago
Strong disagree. enums + unions = ADTs. Without enums you have to use either string or number.
The problem with using strings is that nothing else uses strings (like say, your database), so you have to have this interminable roundtrip conversion between strings and integers.
JS programmers are in a bit of a bubble where using strings as enums is socially acceptable, if a C programmer saw this code
typedef struct {
const char* type;
} Animal;
they would yell at you.
If a database engineer saw this schema:
CREATE TABLE animal (
id INTEGER PRIMARY KEY,
type TEXT
)
they would yell at you.
It's way nicer to use integer enums from A to Z, because everything else has enums and people use them.
If you use plain integers instead of integer enums then the compiler has no visibility into what your tags are. So your diagnostics will be hard to read because the compiler doesn't know that 69 is AnimalType.frog
- chpatrick 2y agoI think string enums are actually totally okay considering they're interned and basically just pointers at the VM level, and then when you're debugging you don't need to figure out what the hell 0x72838ABC is. Pointing to a lower level language with a different use case doesn't justify int enums to me.
- Waterluvian 2y agoThat’s pretty much the last straw for why I switched to string unions. Debugging integers in logs was terrible.