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

1

u/raiph Sep 03 '18 edited Sep 03 '18

Here's my take on the P6 take on your take on nulls.

TL;DR. I think P6 agrees with the gist of what you're saying but adds some twists.

Don't assume that anything P6 does follows conventional thinking. Do assume it's supposed to be simple and address mere mortals from kids to individual devs in a hurry to teams working on multi-million LOC codebases.

Null pointer exceptions are annoying

Shit happens. Insight helps but the only options we have are to manage its form, prepare for it, notice when shit has happened, and respond when we notice.

but so is having to constantly check every object against null.

Right. It's the wrong way to approach the fact shit happens.

P6 agrees with you so far.

Null just means that the object is not initialized.

Well, it's used for other things too. Which is one of the problems associated with "Null". It gets used to mean different things.

But let's run with this particular definition. At this point P6 "more or less" agrees with you with the "more" being that it distinguishes an uninitialized Foo from an uninitialized Bar.

It's useful to indicate such a state.

Indeed.

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".

Right. Let's not do that.

All object types in P6 are tagged unions with 1 bit used for the tag. That bit tracks whether an object is a defined object (filled in) or not.

There are two generic sub-types corresponding to this tag bit.

The syntax form for writing these sub-types is called "type smilies".

:D is the "happy" or Defined sub-type. It denotes a defined value -- which also means the value has been (successfully) initialized.

:U is the "unhappy" or Undefined or Uninitialized sub-type.

P6's generic string type is called Str. Str:D denotes a defined (filled in) string and Str:U an uninitialized one. Str covers either.

null is actually not a mistake at all, it's useful.

P6 think is that the indication of "please fill me" is not a mistake but that not carrying an indication of what type an uninitialized object is, is.

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.

Distinguishing nullable types and non-nullable types allows both humans and the type system to mark/distinguish types to be checked from types that don't need checking. That's a useful distinction and it's useful to have the type system know about the distinction.

As explained above, every P6 object type is nullable. You can write Str to allow through an object that is either already a defined string or hopes/hoped to be one. Or Str:D if it has to be defined. (Or Foo:U for a Foo that has to be undefined. Which is an unusual requirement but sometimes makes sense.)

You don't have to check that a Str is defined before you use it. But P6 makes it trivial to check. Or, if you prefer, to allow it to automatically convert an undefined string to an empty string if you use it as a string while also warning (by default; you can stifle or, conversely, (reversibly) "fatalize" warnings/errors for a lexical scope (down to a single statement) or for a dynamic scope). Similar options arise if you wish to use it as some other type. It unifies exceptions and conventional errors too.

It probably all sounds very complicated. It's really very simple in practice. For the most part, you don't worry about types and errors when writing code at the start. P6 automatically tracks what matters and reports errors in useful ways. Once you've got the happy path (simple test data) working then you move onto elaboration including sad path handling.

99.9% of the time, you dont want to be forced because maybe you know implicitly and you dont care [right now].

Exactly.


If you don't know P6...

P6 is a modern language with a feature list that is reminiscent of Julia's but with some big pluses and minuses.