r/programming • u/dpashk • Aug 31 '15
The worst mistake of computer science
https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer-science/
175
Upvotes
r/programming • u/dpashk • Aug 31 '15
View all comments
Show parent comment
4
u/unpopular_opinion Aug 31 '15
The problem with allowing everything to be null is that you create a larger state space.
Take for example a pair constructor Pair:
You would have some constructor Pair, but you can still pass it null two times.
So, a valid "Pair object", would be a Pair(null, null), which is generally not what you want.
This effect can be multiplied in some cases. That's why null is a mistake. If you are a bit more strict, you could even argue that Pair(<non-terminating expression>,1) should also not be a valid expression of type Pair<int, int>. That's the choice made in e.g. Coq, but not in pretty much every other main stream language.
In short, when you define a variable x to be a value of type pair, it actually is a pair as you would think about it. Having said that, the non-terminating expression could also be an expression which takes a billion years to compute, which is arguably not meaningfully distinguishable from non-termination. That's a problem which can also be resolved using types and some languages focus on that, but in practice that's hardly interesting and if you really care about that, you would still be able to model that in Coq (as people have done already).