I keep hearing people talk about how non-nullable reference types would solve so many bugs and make programming so much easier. Even the creator of null calls it his billion dollar mistake, and Spec# has introduced non-nullable types to combat this problem.
EDIT: Ignore my comment about Spec#. I misunderstood how it works.
EDIT 2: I must be talking to the wrong people, I was really hoping for somebody to argue with :-)
So I would guess, being in the minority, that I'm wrong, but I can't understand why this debate has any merit. I see null as a bug-finding tool. Consider the following:
class Class { ... }
void main() {
Class c = nullptr;
// ... ... ... code ...
for(int i = 0; i < c.count; ++i) { ... }
}
BAM! Access violation. Someone forgot to initialize c
.
Now consider this:
class Class { ... }
void main() {
Class c = new Class(); // set to new Class() by default
// ... ... ... code ...
for(int i = 0; i < c.count; ++i) { ... }
}
Whoops. The loop gets silently skipped. It could take a while to track down the problem.
If your class is empty, the code is going to fail anyway. Why not have the system tell you (albeit slightly rudely) instead of having to figure it out yourself?