14

A nullable boolean in production 😕😯😤

Comments
  • 5
    I *definitely* haven't done this before when I've been feeling lazy and needed a value to have three distinct states...
  • 10
    Nullable 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.
  • 2
    @Root it's being used for two states 🙄
  • 0
    @TheMaesterio careful using it as two states container. it could render the server down. done it before
  • 3
  • 1
    @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.
  • 1
    @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).
Add Comment