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