Live data from Hacker News

What if null was an Object in Java?

donraab.medium.com

141–150 of 164 posts

Re: What if null was an Object in Java?

#141
post #130

Earlier quoted context omitted.

Scope functions are still huge. Yes, deep let chains can certainly be considered an antipattern (sometimes I like the approach of writing them, then transforming to more imperative for readability, I think readability peaks at a mix of imperative with some shallow .let), but I miss them in any language that is not kotlin. Variables should be the exception, not the norm. I have no patience for names that designate som…

> Scope functions are still huge They can be useful, but I can never remember which one of let, run, with, apply, and also I currently need. Also, I've noticed that they motivate overly "clever" code, especially but not exclusively in junior programmers.

They absolutely do come with a goldilocks problem attached, best used in moderation, and require a bit of an aesthetic to develop. But what doesn't. What appears "clever" to the reader is actually dead simple train of thought coding at write time, with many types of bugs waiting to bite in an imperative implementation simply not possible.

That's why I like writing scope-heavy and then transforming to an aesthetic middle ground between scope expression and imperative. Name stuff when you have something meaningful to say, when there's something helpful to express in a name, not when the syntax mandates a locally unique ID.

Re: What if null was an Object in Java?

#142

The problem isn't really that null isn't an object, it's that the type system allows any object reference of a particular class to also be null. After using Typescript for the past couple years, it's such a joy when you define a variable of type Foo and know it won't contain null. Granted, TS also has to deal with undefined vs null weirdness (and even more weirdness in that an object not containing a property is subt…

> having a method return an Optional that will sometimes also return null

Wow that's terrible. When Optional landed in Java (8?) I came to the conclusion that the point was to use it as a contract from the developer of a method: "I return an Optional in this method so I guarantee it won't ever be null". If this is not respected, there's absolutely no reason to use Optional.

Re: What if null was an Object in Java?

#143

Earlier quoted context omitted.

It's funny because I really dislike the C# approach. It's not truly enforced - any code with nullable disabled can call public functions with `null`, even if on your side they were marked as non-null.

This is true, but you can fairly easily audit your solution for files/projects which have it disabled. And yeah, you need to verify the data at the boundaries of your trusted code - but that's the same situation as it's always been. Where NRT help is avoiding boilerplate null checks in the dense forest of your business logic.

Well my point is more that if they introduced an entirely new non-null reference type to the language then you could never make that mistake - it would be non-optional for those calling into your code to respect whether null can be passed in or not based on the type you used. Since they instead made it an optional system you can not rely on those using a library you make to actually turn it on, you have to accept that null _can_ be passed in even if you mark something as non-null according to the system on your side, which is dumb.

Re: What if null was an Object in Java?

#144

Earlier quoted context omitted.

It's funny because I really dislike the C# approach. It's not truly enforced - any code with nullable disabled can call public functions with `null`, even if on your side they were marked as non-null.

Yes, that is possible but you still get warnings and you should compile with warnings as errors anyway.

You only get warnings if you turn those on, you can use `disable` and then the language works exactly as it previously did. In fact, when you disable it you get warnings if you _use_ the nullable system and mark a reference as `?`.

Re: What if null was an Object in Java?

#146
post #142

The problem isn't really that null isn't an object, it's that the type system allows any object reference of a particular class to also be null. After using Typescript for the past couple years, it's such a joy when you define a variable of type Foo and know it won't contain null. Granted, TS also has to deal with undefined vs null weirdness (and even more weirdness in that an object not containing a property is subt…

> having a method return an Optional that will sometimes also return null Wow that's terrible. When Optional landed in Java (8?) I came to the conclusion that the point was to use it as a contract from the developer of a method: "I return an Optional in this method so I guarantee it won't ever be null". If this is not respected, there's absolutely no reason to use Optional.

Totally agree, which is why I was baffled when I saw this code. But it goes to show that if something isn't enforced at the compiler level, "clever" developers will find a way around it.

Re: What if null was an Object in Java?

#147
post #74

What if every null reference was an instance of a Null Object pattern? https://en.wikipedia.org/wiki/Null_object_pattern Eliminates null checks, Optionals, and NPEs. Probably moots annotations like @NotNull too, but maybe some use cases need those for client APIs. Makes iterating data structures like graphs simple. Faster too, because the JIT NOP a Null Object's methods. (Mostly; profile to confirm, then tweak as nee…

Not everything has a sensible null object. It also means that forgetting to set a value will often silently give wrong results instead of immediately producing an error.

Apologies, I omitted part of my proposal:

While most Null Objects would (probably) be code generated, you can provide your own as needed.

Said another way: Most of my Null Object implementations have been boilerplate, and therefore could be code generated. But occasionally some are not.

Re: What if null was an Object in Java?

#148

What if every null reference was an instance of a Null Object pattern? https://en.wikipedia.org/wiki/Null_object_pattern Eliminates null checks, Optionals, and NPEs. Probably moots annotations like @NotNull too, but maybe some use cases need those for client APIs. Makes iterating data structures like graphs simple. Faster too, because the JIT NOP a Null Object's methods. (Mostly; profile to confirm, then tweak as nee…

Objective-C does that but it has the effect of separating serious consequences from their causes. Places that really require non-null have a much more difficult time finding where the null value originated. And since Objective-C contained a lot of C, it really just separated null dereferences from their causes making debugging much harder.

Thanks for replying. I really appreciate pushback, criticisms, and thoughtful opinions.

I don't know Obj-C, so I'll have to research those use cases, to understand better. eg: Are those across processes? Is this an Obj-C idiom? How to make illegal state representations impossible?

Per the Null Object pattern description, Null Object captures all of the null handling and behavior in one place, vs scattered around. Stuff like null checks and throwing exceptions. It does not eliminate the need for the concept of null.

I'm probably (mostly) wrong about eliminating @NotNull annotations. Especially in method signatures (defensive programming). Last night while commenting, I was thinking about annotating POJOs, to info serialization.

Re: What if null was an Object in Java?

#149
post #74

Earlier quoted context omitted.

Not everything has a sensible null object. It also means that forgetting to set a value will often silently give wrong results instead of immediately producing an error.

Apologies, I omitted part of my proposal: While most Null Objects would (probably) be code generated, you can provide your own as needed. Said another way: Most of my Null Object implementations have been boilerplate, and therefore could be code generated. But occasionally some are not.

What I mean is that semantically there's often no such thing as a null object for a given domain. So any artificial one that can be made is indistinguishable from null in practice (i.e. all operations on it will throw etc).

Re: What if null was an Object in Java?

#150
post #6

Earlier quoted context omitted.

There's no reason that something can't both an object and the bottom of the type hierarchy. It's, technically, an instance of multiple inheritance. Java doesn't generally allow this, but there's tons of special cases in the compiler for things that you can't do yourself. For example, defining operators is done in the compiler, but you can't define operators for your own classes.

It would be a bottom-type of reference-types. This wouldn’t work for value-types like int, at least not without boxing, which would be very painful.

But an int can't be null. If a method takes or returns an int it's guaranteed to be not null.
Post reply on HN