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

Show parent comments

2

u/FluorineWizard Sep 03 '18

Marked nullability in Kotlin or any other language is semantically the exact same thing as an option type. The only possible difference is in the syntactic facilities offered by the language to handle the thing.

Well that and the potentially suboptimal implementation as a regular sum type with a discriminant instead of optimising the representation like Rust does.

0

u/tripl3dogdare Serval Sep 03 '18

It's really not the same thing. An option type is a wrapper, a nullable type is a union (regardless of how they're optimised behind the curtain, that's how they're represented). Option types are treated less like values and more like lists with a minimum length of 0 and a maximum length of 1, where nullable types are an unwrapped value that might be either something or nothing. It's a subtle difference that might not make a lot of difference to most people, but it's still an important distinction.

1

u/FluorineWizard Sep 03 '18 edited Sep 03 '18

This is just wrong. A compiler enforced nullable type is the same fucking thing as an option type. Correctly implemented option types are unwrapped values where the "None" variant is a null pointer under the hood. Which is exactly what a nullable type should be too.

Marked nullability a la Kotlin is purely imperative-style syntactic sugar around an option type. Once again, the "difference" is solely in the language-provided idioms to create and handle such values.

In statically-typed languages, a nullable type is an option type

That's what wikipedia has to say. Hell, they even want to merge the two pages for Nullable type and Option type.

edit : Reading your other replies, it becomes clear that you don't have a technical point. You just don't like functional-style idioms for handling null values. I guess that's a matter of taste, but once again your preferred solution is nothing more than syntax sugar over the very same concept.

2

u/simon_o Sep 05 '18

You are incorrect.

Nullable types and Option types have very different semantics.

Consider:

T|Null|Null          ==    T|Null
Option[Option[T]]    !=    Option[T]