54

There are in fact three unique Boolean values.
Their names are "true", "false", and
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
...

Comments
  • 6
    I actually used a 3-states boolean in a database: true, false or null.

    Let's be honest: that was an ugly hack, not proud of it.
  • 4
    @willol That can be useful, actually, e.g. to indicate whether the user has been asked for the data yet.
  • 0
    @Ashkin I don't remember the exact use case, but it began as a simple boolean, and a third possibility came later... No excuses, honestly :)
  • 9
    3 Boolean values?

    JavaScript: "Hold my beer"
  • 0
    @Ashkin use enum in this case
  • 0
    @Ashkin how would that need 3 states? It is either asked or not. That's true and false. Or am I missing something?
  • 0
    LOL hope you get that stressball
  • 6
    @Charmesal Let me share an example to explain:

    Due to developer incompetence, newsletter subscriptions were stored, not in the User table, but on another server entirely. When he was fired, we lost access to that server -- and most others. (He refused to share credentials...).

    To remedy the newsletter situation specifically, I added another column to the User table: bool subscribed_to_newsletter. This defaulted to null, indicating "not set." When the user accessed his/her profile and this col was null, the site would ask them specifically if they wanted to subscribe or not, and record their answer as true/false. This allowed me to record both bits (asked and subscription status) within a single column in a very obvious and easy-to-understand way.

    (Adding a second column would have also worked; however, the column would be useless in the future. As the question is present on the registration page, all future registrations would have a true/false subscription status set.)
  • 3
    @Ashkin where others see ugly I see genius.
  • 0
    @vrpg1998 Look at TypeScript, it is very similar to JavaScript but with types.
    But to work in Browser you need a TypeScript compiler.
  • 3
    @Ashkin I can see why you would use it in that manner. A bool being null still feels weird to me but I get it and I understand why you would choose that. Thanks for the explanation.
  • 0
Add Comment