3

Doesn't the existence of NULL in a language mean, that there cannot be a boolean type? Because a bool is defined by the fact, that it can have two possible values. Having NULL changes that.

Without NULL a variable of the type bool could be True or False, two possible values. With NULL is can be True, False or NULL, three possible values.

I hate NULL.

Comments
  • 1
    doesnt NULL become FALSE anyway as it is not TRUE
  • 0
    @C0D4 who knows? If the returning method could know if the value is True or False if would not return NULL in the first place. There is no logical way to process NULL - except throwing an exception.

    Casting it into False is as invalid as casting it into true.
  • 4
    in Java you have two types of boolean.

    - boolean (primitive) can be either true or false.
    - Boolean (object, a boxed primitive) can be either TRUE, FALSE or null.

    This in fact gets very handy!
  • 2
    Depends on implementation IMO. A proper null is not a value, it's an indication of a lack of value. Kotlin handles it nicely: you have to allow nulls explicitly and then you have to deal with them explicitly. At the same time, Java's null is terrible.
  • 0
    A boolean can never be NULL, it is always true or false.

    If a NULL is converted to boolean, it usually evaluates to false. But if it can be NULL, it's not a boolean.

    There is in fact something called Tri-State, which can have three states:

    - True

    - False

    - Don't care

    But Tri-States are usally used in electronics (high signal, low signal, high impedance), they are not common in programming languages.
  • 1
    I reckon in MySQL that Bool is actually a tinyint(1) and should be casted in the code anyway. Though, I'm pretty sure, when you look for false values, you'd want to check for both NULL and 0 values when looking for false. (eg. I think it ignores the NULL values when looking for "<1")
  • 1
    @xewl Unless maybe you have a non-nullable column or a `default false`? :)
  • 0
    @netikras if it isn't Nullable, then there's still the possibility for > 1 values :/ - I'd rather use enum in that case tbh
  • 2
    @xewl in mysql Null is always treated as Null.
    Any arithmetic operation will result in null.

    That’s why we have <=> for null safe comparisons.
  • 1
    @C0D4 There, I should try that ö, been using <>
  • 2
    @UnmutableToday in loosely typed languages null will become false.

    If(null == true) # false

    Strict/strong type languages throw exceptions for null pointers which is the correct behaviour.

    @xewl
    https://dev.mysql.com/doc/refman/...
    This thing has made life easier when working with nulls
  • 0
    @xewl isn't ´<>0´ == true? Depends on orm I guess, but it's the case in plain C.
  • 0
    @netikras Well, in this case it's MySQL, and if I check for "<> 1", It wouldn't match with NULL values (if the field is Nullable), only with any numerical value other than 1, thus eg. 0, 5, 888...; as you're checking against a numerical value.
  • 0
    nah, I mean if the column is non-nullable (ergo no nulls will be ever there) and you're checking whether column `<> 0`

    null values -- N/A (because of the constraint)
    0 <> 0 == false -- there you go
    1 <> 0 , 500 <> 0 , -436356 <> 0 == true -- because anything but 0 is not 0 :)
  • 0
    Not an expert but in C# you also have nullable types which allows you to have Nullable<bool>. You can also declare them as bool? (with the question mark). We used that kind of variable because some fields in the database were neither true nor false, they were null. And this would avoid giving us an error when getting the value of the bool.
  • 0
  • 1
    A boolean variable is always true of false. If it is null, this means that the boolean variable doesn't exist.
  • 0
Add Comment