Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
I *definitely* haven't done this before when I've been feeling lazy and needed a value to have three distinct states...
-
Root797606yNullable bools are amazing when you need three states.
example: yes, no, haven't asked yet.
It isn't a bad pattern at all unless you only need two states. Right tool, right job. -
@TheMaesterio careful using it as two states container. it could render the server down. done it before
-
@Root Despite my previous confession, I'd almost always pull someone up in a code review for using a nullable boolean. There's very few scenarios where an enum wouldn't be a better option - a boolean that uses null as a valid state looks a bit too much like an accident IMHO.
-
Root797606y@Proximyst @AlmondSauce
Enums make sense for scenarios where you will want to extend something later, or where answers require something more specific than true/false. Replacing a bool with a true/false enum is silly. (Unless it's a pattern for the project; that would make e.g. reporting easier)
When you can answer a question yes, no, or (no answer), nullable bools are perfect.
If you're using only two of the states states, that's bad. Use a bool instead. If you're using the third state to mean something other than (no value), you should be ashamed and should go work on WordPress.
Their use is narrow, but they are the best tool for those situations (and admittedly, I have found quite a few situations where they're perfect).
A nullable boolean in production 😕😯😤
rant
maintenance heaven