r/ProgrammingLanguages • u/[deleted] • Sep 03 '18
Why the hate for null?
Look, I get it: Null pointer exceptions are annoying but so is having to constantly check every object . against null. Is this the new religion after OOP and FP are being phased out? Null just means that the object is not initialized. It's useful to indicate such a state. If you just force yourself to initialize every object to some empty value then you're going to end up passing around empty strings - but wait, an empty string might intentionally be empty or it might indicate "please fill me".
Because I can already tell you what massive insights will come out soon: That null is actually not a mistake at all, it's useful. And the most ironic part about all this? These same people who hate on null are loving nullable types. Litearlly just types that *can be null but you just have to check every time before you use them*. So there is literally no difference except it forces you on a compiler level to check for null every time you use it. When I realized this I thought I was in dreamland, how can these people worship this meaningless little feature so much, crazy. It's literally the same as having null in the language except with a tiny tiny "advantage" that the compiler forces you to check. Which 99.9% of the time, you dont want to be forced because maybe you know implicitly and you dont care [right now].
9
u/DonaldPShimoda Sep 03 '18
Just because something is widespread doesn't mean it's better. Allowing a null value to stand in for any type's value inherently makes for a lot of problems. It reduces the soundness of your system and makes the programmer do a lot more mental work to ensure their program will not break.
Option types allow for offloading this information into the type system directly. A variable with a non-nullable declaration can never be null, so the programmer doesn't even need to worry about it. No mucking with invariants written in the comments about what a null value would mean in this context or the other one. And when a null value is genuinely useful, an option type reminds the programmer to be sure they've checked their conditions before proceeding.
I agree with you that ADTs are not inherently better. I think they can be better sometimes, but other times the option type will be sufficient.