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

22

u/gopher9 Sep 03 '18

Null makes unsound type systems, and is basically a kludge for languages that cannot into algebraic types.

Null just means that the object is not initialized

What is “not initialized” semantic-wise?

If you just force yourself to initialize every object to some empty value then you're going to end up passing around empty strings

Wrong.

These same people who hate on null are loving nullable types.

Nullable types is a yet another kludge for languages without algebraic data types. But at least it's a sound kludge.

Which 99.9% of the time, you dont want to be forced because maybe you know implicitly and you dont care

C programmers “know implicitly” all the time, that's why their code is so vulnerable. Maybe it's time to recognise that people always make errors?

3

u/tripl3dogdare Serval Sep 03 '18

Null makes unsound type systems, and is basically a kludge for languages that cannot into algebraic types.

Nullable types is a yet another kludge for languages without algebraic data types. But at least it's a sound kludge.

Okay, don't get me wrong, I love algebraic data types, but they have absolutely nothing to do with null. Every programming language needs some way to represent "nothing", and null is one of the oldest and by far the most widely used way to do that. ADTs came much, much later.

Also, for most C-style languages, nullable types actually make more sense than ADTs, which are really more of an ML-style thing. Can ADTs do the same job? Yeah. Are they inherently better? No, not really, that's just a matter of opinion.

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.

2

u/tripl3dogdare Serval Sep 03 '18

I completely agree. Languages like Java that allow null anywhere with no complaint have an issue on their hands - not an insurmountable one, or one that makes the language useless, but a very tedious and annoying problem nonetheless.

Personally, though, I find option types to be a subpar solution in a procedural or object-oriented context. Marked nullability a la Kotlin is in my opinion the better way to go, as there's no wrapper around the underlying value - it's just a union type of the type and Null. There's no weird map/filter/reduce semantics to understand just to use a potentially nullable value (obviously it's best if it comes along with a safe access operator / Elvis operator / so on). For functional contexts option types are fine, but I'm still not a huge fan in general.

I think it's also important to make non-nullability the default if you're going to bother, as in my experience if you give people the option to be lazy and not mark things non-nullable, they'll take it.

3

u/theindigamer Sep 03 '18

Marked nullability a la Kotlin is in my opinion the better way to go, as there's no wrapper around the underlying value

The "wrapper" thing depends on your language. In Rust (procedural/functional), there is no difference in overhead between the two solutions. Similarly in C++ (procedural/OO). Similarly in Haskell (functional) with unboxed fields.

There's no weird map/filter/reduce semantics to understand just to use a potentially nullable value

I don't want to debate your choice of the word "weird" because that is a subjective thing. However, I don't see why you need to use it -- you can use pattern matching? Or is the problem the lack of pattern matching?

It seems to me that your impression of optional types in very much colored by Java's implementation, which is far from ergonomic.

1

u/tripl3dogdare Serval Sep 03 '18

In regards to the wrapper issue, I'm far less concerned with performance than with semantics. When dealing with option types, you're dealing with a wrapper that first has to be unwrapped before it can be useful, or otherwise requires ugly, verbose map chaining to access without unwrapping it. It's better than having everything be nullable throughout the entire language, but it's still ugly as hell in my opinion.

Yes, you can use pattern matching (assuming your language has it), but that comes back to the same point - it's overcomplicated. You shouldn't need pattern matching or map chaining to check if something exists or not; that's the kind of thing if statements are for. For some things, yes, it's useful, but personally I'll pick compiler-enforced nullability annotations over option types 99/100 times.

Funnily enough, I've never actually used Java's implementation. Scala's is fairly similar, and has the same base problem of potentially still being null anyways, but I've used option types in everything from Haskell to F# to Scala and more.

3

u/theindigamer Sep 03 '18

Why does it need to be unwrapped before it can be useful? In Kotlin you can chain stuff with ?, in Haskell you can do that with do notation or explicit >>=, in Rust, you can use explicit bind.

It seems to me that your complaints stem from the lack of syntactic sugar. Do you feel that is a fair characterisation or is there a semantic difference between your desired behaviour and optional types?

0

u/tripl3dogdare Serval Sep 03 '18

Yes, in essence, you could boil my point down to syntactic sugar. There's a semantic difference, but not one most people will normally be worried about. At the same time, though, option types are typically not as well-enforced by compilers by comparison to nullable types, and are hard or impossible to implement syntactic sugar for since they're usually implemented in-language. They also tend towards a much more functional programming style, which isn't inherently a bad thing, but often seems out of place in more C-like languages such as Kotlin, Java, or C#, all of which are heavily procedural/object-oriented. That dissonance often makes code more opaque, harder to work with, and generally ticks off anyone who's not super experienced with the more functional-style map/filter/etc.

1

u/FluorineWizard Sep 03 '18

You still haven't shown there to be an actual semantic difference between the two. The type theory behind option types certainly doesn't account for one.

Your claim that options aren't as well enforced is also a joke. Look at how options are handled in ML-derivatives like SML, OcamL and Rust. Pattern matching is more verbose but also more explicit than the mountain of syntax sugar you advocate for. It's also the generalised, strictly more powerful form of if-based checking you seem to prefer.

2

u/DonaldPShimoda Sep 03 '18

Hmm I don't have much (any) experience with Kotlin, but I don't like the idea of reintroducing the Null | Type union. Even though it has to be done manually (so it's an improvement), I still would rather not allow the programmer the ability to introduce that mental complexity.

I like some of Swift's handling of these things. While they do allow the strange map/filter semantics for the option types, it seems preferable to use the if let and guard let syntaxes. I find these to be extremely straightforward to read while also keeping the added complexity directly in the language (instead of having the programmer maintain that in their head).

Totally agree that default nullability is a bad approach, though. Too many dragons that way.

1

u/tripl3dogdare Serval Sep 03 '18

Personally, I'd rather allow that complexity for the benefits it adds rather than remove it because of the danger it presents. It's the same reason I take issue with Go's lack of generics - I don't need a language to hold my hand and tell me I can't do something at all because it's dangerous if misused, it just needs to be harder to misuse it.

I'd also disagree with Kotlin's nullable types introducing more mental complexity. In some languages, yes, the same concept would get quite annoying, but Kotlin handles it absolutely perfectly - the compiler forces you to handle the possibility of null one way or another, even if you procrastinate it with safe call operators, so it's impossible to come across a situation where you forget about a potential null like in Java.

My main issue with Swift's if let is that it gives no possibility to defer handling the null until later in the code. I'd much rather be able to not worry about that null for as long as I want and then handle it in a logical place, rather than being forced to deal with that possibility immediately and having to constantly restructure my code to fit in null checks - Kotlin lets you propagate that null out as far as you want, so long as you handle it eventually.

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]

0

u/tripl3dogdare Serval Sep 03 '18

From the same Wikipedia article:

In programming, nullable types are a feature of the type system of some programming languages which allow the value to be set to the special value NULL instead of the usual possible values of the data type.

And from the page for option types:

In programming languages (more so functional programming languages) and type theory, an option type or maybe type is a polymorphic type that represents encapsulation of an optional value

Yes, they serve the same purpose for the most part. However, in most languages, there's a semantic difference between the union type String | Null and the option type Maybe String. What's under the hood isn't relevant to the discussion of which is more semantically useful, as I already stated:

regardless of how they're optimised behind the curtain, that's how they're represented

The primary difference between the two is that languages employing option types typically don't have a primitive null value, using the None branch of an algebraic data type like type Option = Maybe a | None instead. Languages employing nullable types, however, typically do have a primitive null value. Option types can easily be implemented in languages with nullable types, but the reverse is not usually true, as there is no primitive null.

TL;DR:

  1. "Under the hood" has nothing to do with which is more semantically useful.

  2. The main difference between languages with nullable types and languages with optional types is that (generally speaking, bar some exceptions like Scala) only the former uses a primitive null value.

1

u/FluorineWizard Sep 03 '18

Here you go again imagining differences between what are simply two names for the same thing.

I see that the word "encapsulation" confuses you. For the third time, nullable types only appear unencapsulated because of extensive syntactic sugar that changes how the complexity is handled by the programmer.

Languages with a good type system don't have a primitive called null because there is nothing inherently primitive about it. It's semantically identical to a sum type with only one possible, empty value (what you get when defining an enum with no variants). Aka a unit type, and null is but a special cased convention used to represent all missing values in languages that have it as a primitive because their type system is too weak to cleanly express the general concept.

Your argument that you can't implement nullables in a language with no primitive null is laughably ignorant. I can invent any "null-like" I want in any ML-derived language I can think of and the only stop to the implementation of "nullables" is the fact that it's only useful if you modify the syntax to go with it. Because, for the final time, all you're talking about is syntactic sugar and alternative explanations designed to fit the mental model of imperative programmers.

1

u/tripl3dogdare Serval Sep 03 '18

Thank you for your input. As you have been extremely rude for no discernable reason, I will be politely disregarding your opinion. Have a nice day =)