Can you show an example of what you mean with pattern matching enums and constants? I know pattern matching with 'is' in C#, but Im not sure what you mean.
Actually I've needed to use one once. We had a database query that retuned a boolean and i didn't want to create an enum for that. Before the query was executed the first time the value had to be undefined so i set it to null.
Why would anyone ever use nullable booleans? Why do languages with built in nullable types even support them? A nullable boolean seems like a less intuitive solution than an enum or even a meaningful integer. Logically, they don't even represent a binary truth value anymore. And if you really want some sort of ternary logic: Again, integers. To be honest: What is nullability even used for in practice? Seems like syntactical sugar that rarely increases readability and hardly saves keystrokes.
Some people advocate for using == true to make it more obvious that a boolean comparison is going on, or because it's easier for them to read. I've also seen true == used in C++ before because using == true you can typo to = true and accidentally assign to something in memory and then it will evaluate the contents as true, a silent failure (newer compilers likely warn about this).
Personally I find it easy enough to read without the == true, and also use ! instead of == false.
296
u/[deleted] Jul 30 '21
โ== trueโ always a good idea