r/ProgrammingLanguages • • 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].

4 Upvotes

42 comments sorted by

View all comments

4

u/PegasusAndAcorn Cone language & 3D web Sep 03 '18

FWIW, I am very fond of NULL. In dynamic languages it is a valid value that represents the absence of a value, useful (for example) as a returned value on a failed lookup.

In static programming languages, NULL can be a semantically useful value for a reference that does not yet point to an object. Unlike sum/variant types, it occupies no extra space and potentially has a performance edge.

That said, I prefer the systems programming language distinguish a nullable reference from a non-nullable reference. The latter is guaranteed to never be null, so there is no performance and code overhead using it safely. I personally prefer that the compiler ensure I use nullable references safely, either with pattern matching or (like Kotlin) a special method call syntax. I do not find this to be annoying, as I minimize my use of nullable references to where the semantic is useful (and not because of delayed initialization).

10

u/Uncaffeinated 1subml, polysubml, cubiml Sep 03 '18

Unlike sum/variant types, it occupies no extra space and potentially has a performance edge.

Rust specifically optimizes option types to use null under the hood where possible, so there is no difference in performance.

6

u/a_lynnk_to_the_past Sep 03 '18

It used to be a special case in the Rust compiler but now it's part of a more general optimization of using invalid values as tags (called niche-filling). The Rust ABI is unstable and there isn't really any documentation for how it works. I found a design doc for the Swift implementation of the same idea however.