4 ms·
Tri-state booleans are also very ugly. What happens if you suddenly need a 4th state? Use enums (or any equivalent) for states that are non-boolean.
by andix 6y ago
Tri-state booleans are also very ugly. What happens if you suddenly need a 4th state?
Use enums (or any equivalent) for states that are non-boolean.
- hallway_monitor 6y agoIf you are using a nullable bool, you do not need a fourth state. True, False, Unset. Very useful in a lot of cases. It's actually not ugly but very elegant.
- m_mueller 6y agoI don't think it's elegant at all. Especially in SQL dialects where you need to check null state separately from false. So much bug potential...
- Spivak 6y agoWith an enum you’d have to check the unset state separate as well.
- crocarneiro 6y agoHe meant that in some SQL Dialects you cannot check FIELD = NULL, you have to use FIELD IS (NOT) NULL (like Oracle). Therefore you cannot use FIELD inside an IN clause either.
- andix 6y agoBut with an (NOT NULL) enum/integer/string or however you are representing it, every state is a regular case. Not a special case. Null nearly always needs some special treatment.
- jimktrains2 6y agoNull is a signifier that a value isn't know. Yes, if you're using null to mean something else you should use another type, but nullable-booleans (and nullable fields in general) can be extremely useful. For instance, if you're collecting information, but wont know everything at creation time, the unknown values are null.
- chriswarbo 6y ago> For instance, if you're collecting information, but wont know everything at creation time, the unknown values are null. I find the idea of 'creating unknown values' to be self-contradictory. I find it much more logical to define a PartialFoo containing only the parts we know up-front, and a fill in the rest later using a function PartialFoo -> Foo. If we need multiple steps, we can put some Optional fields in the PartialFoo, to avoid lots of intermediate types. This is still annoying, but it keeps all of the 'intialisation headaches' separate from the Foo type itself; so code dealing with Foo doesn't have to care about null checks, missing fields, etc.
- jimktrains2 6y ago> If we need multiple steps, we can put some Optional fields in the PartialFoo, to avoid lots of intermediate types. You juat described the purpose of SQL's NULL.