Null > something.. is this a wtf or am i missing something
After checking my daily wtf i started debugging some code because someone had reported a, surprise, bug.
anyway, context is not really important but ill give it anyway:
MSaccess form, with 2 date fields. Meta-information suggests that date 2 is always after date 1, though not both information shoudl even exist (is about certain irl events, but IF they both took place, date 2 is always after date1)
so there is a check if date2<now() (date shouldn't be in the future) and... date1<date2
-> am i correct in thinking that if date1 doens't exist, it should always be smaller than any date? explicitly smaller than any valid date?
my quick sollution was to just check if date1 was null (vb: IsNull(date1)) before checking for just Now() or before checking for Now() and the other date...
still made me go WTF. is it my weird head, or should the original code have worked?
Isn't NULL fun? It's in the nature of data, since NULL could mean a whole lot of things (from not known, to not applicable, to not entered, etc).
(NULL == someva}) is Unknown
(NULL < someval) is Unknown
... and so on ...
(NULL != someval) is Unknown
.. and even ...
(NULL =? NULL) is Unknown
Unknown is neither True nor False, and should always be explicitly tested.
with the CLR 2.0 nullable types, this is now well defined:
<font size="2">DateTime? nullDate = null; // nullable type
DateTime minDate = DateTime.MinValue;
nullDate == null // true
nullDate < minDate // false
nullDate > minDate // false
nullDate == minDate // false
nullDate != </font><font size="2">minDate </font><font size="2">// true
DateTime? someOtherNullDate = null;
nullDate == someOtherNullDate // true</font>
This lets you define the semantics of the value null for your variables.