June 6, 2004C# nullable types: null == null?Posted 1642 days ago on June 6, 2004C# watchers will be aware that version 2.0 of the language has nullable types. This is handy when talking to databases, as use of null values is common. There's one thing that troubles me though. As I've always understood it, a null value in a database means "value not known". In the real world, an example would be a yes/no question on a questionnaire where no answer was given. Now the part that troubles me: C#'s designers have decided that two null values are equal.. But how can this be? "Value not known" does not mean that there is no value. Returning to the questionnaire illustration, the fact that the question was not answered does not mean it has no answer. Imagine the question: "Will you vote Democrat in the presidential elelection"? Two people both refuse to answer. Null values. Yet both go on to vote; one votes Democrat, the other does not. So did they have the same intentions? No. If a programming language is to represent the real world accurately, then the equality of two null values is unknown. So why has the C# team made this decision? If, as the article referenced above implies, it is merely programmer convenience, then I disagree with the decision. Convenience is less important than correctness. I can see a real possibility of error here. On the other hand, as the article also says, having the relational operators return a nullable bool would be fundamental change. I don't know what the answer is, but I'm blogging to record my unease. Re: C# nullable types: null == null?Posted 1642 days ago by Brett Alcorn • • • Reply
There is a more fundamental rule for me, which is that == and != should return opposite values. Boolean logic is pure and powerful and hence it seems a violation to have a value which is neither equal, nor un-equal, to null. That is: Re: C# nullable types: null == null?Posted 1642 days ago by Tim Anderson • • • Reply
Darren, Re: C# nullable types: null == null?Posted 1620 days ago by Hassan Voyeau • • www • Replynull = null makes perfect sense to me. Both variables value are unknown. And, I also agree with the point that null == null and null != null should not be the same. |
Recent postsUsers plead with Borland to give up .NETIE7 to be released 18th October,... If Microsoft doesn't use UAC, why... Google's unsettling lack of direction Vista security: now prove it |
Re: C# nullable types: null == null?
Posted 1642 days ago by Darren Oakey • • www • Replya) null = null in the real world is the same. Take the voting analogy. If you are looking at people coming in who take a paper, and they have to vote yes or no, then you will sort into three different classes "no", "yes", and "didn't submit a valid vote". Is one blank paper the same as the other? YES, absolutely, whatever the people who chose not to vote were thinking at the time... In the real world, in every meaningful way, all "null's", or lack of response, are exactly equivalent. Your arguments about the intentions of the action have no merit - otherwise we'd just say that two "yes" answers weren't the same, because the person voted for different reasons!
however:
b) I think there is a very odd statement in your assertion - "if a programming language is to represent the real world accurately".
Since when does anyone use a programming language to represent the real world? That's an absurd concept! Programmers use a programming language to cause a desired action to happen on a computer. Higher level languages are written purely and simply to make that "instructing" be as easy as possible.
So - even if (which absolutely ISN'T the case) - nulls weren't the same in the real world, it would still be insane for language designers to differentiate them on the computer.
There are literally zero possible ways that having null!null could be used as a programmer. In fact, the number of ways in which it would screw you up is astronomical... Every statement which used object comparison would have to separately deal with nulls, and if the programmer failed to deal with it, you'd have worse problems - because even though you might assert that null = null shouldn't be true, you DEFINITELY couldn't imagine that null != null should be true - imagine all the things that would fall over because programmers have put in completely sensible logic, but nulls are not behaving sensibly.
And, on the other side - null = null is _very_ useful. You can test for it. Otherwise how _do_ you say, "is this not present?" How do you compare two records which are identical in every way, but some of there values are missing?
Think of all the hundreds of thousands of man-hours and dollars that have been wasted by some idiot, (hanging's too good for him) - deciding that nulls can't be equivalent in SQL. Even though most modern databases are fixing this problem - let's not make such a costly mistake ever again!!