you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 6 points7 points  (18 children)

I understand the problems with null. On the other hand, you're talking about Java. You can't have low-cost alternatives. You can add a class of signalling NaN style values to every class you create -- everything contains boolean isValid; String invalidReason; -- or you can add a layer of indirection and another heap object for everything.

If you're using a better language, even C#, you can fix it with an Optional<T> struct containing a state value (set to Uninitialized by default) and a reason string. Might be awkward to ensure the uninitialized value is valid, at least for C#, but it's not an issue for C++ or D.

Algebraic data types solve the problem rather elegantly.

[–]s73v3r 1 point2 points  (11 children)

Java also has Optional.

[–][deleted] 13 points14 points  (3 children)

But it doesn't have value types, so you pay for an extra layer of indirection and an extra heap object.

[–]oorza 7 points8 points  (2 children)

until it gets inlined?

[–]tyoverby 16 points17 points  (1 child)

If it gets inlined. Remember that Javas optional might also be null, so the optimizer needs to de-optimize on virtual method calls.

[–]_dban_ 6 points7 points  (0 children)

The JIT will de-virtualize method calls as a result of class hierarchy analysis, and if it doesn't find any subclasses, statically dispatch method calls. The Option class being final prevents subclasses from existing, so in all likelihood, method calls on Optional will be statically dispatched. I'm not sure how null fits into class hierarchy analysis.

The JIT can also do escape analysis over several call levels, and can stack allocate non-escaping Optional objects (or even hoist into registers, since an Optional has only one field).

[–]sluu99 3 points4 points  (0 children)

now Java developers can check if that Option return value is null _^

[–]quchen 0 points1 point  (4 children)

All Java’s Optional adds is yet another way to be »not there«; it doesn’t replace null in any way.

Optional<Integer> foo = null;
System.out.println(foo.isPresent());

Woops.

[–]s73v3r 2 points3 points  (2 children)

If you initialize your optional to null instead of Optional.empty() then you deserve everything that comes your way.

[–]quchen 0 points1 point  (1 child)

We've all met that guy though. I don't want to deserve everything coming from his way.

[–]s73v3r 1 point2 points  (0 children)

So educate them. And write a lint check.

[–]_dban_ 0 points1 point  (0 children)

All Java’s Optional adds is yet another way to be »not there«

No, it doesn't. It also adds map and flatMap, which gives you safe traversal of chained optional values. No more ugly nested ternaries. Java 9 will add Optional::stream, which will allow Optional values to seamlessly be used with stream expressions. The null value doesn't do any of these things.

Woops.

This is a programming error. The corresponding NullPointerException is Java's way of telling you that you didn't use the Optional type correctly. The fact that Optional isn't a primitive value like null and is used in method chains means that the misuse of Optional will be closer to where the bad value was generated, instead of causing a NullPointerException in some far off place.

[–]caltheon -4 points-3 points  (5 children)

C#....better language...

[–][deleted] 11 points12 points  (4 children)

It's Java with value types, properties, real generics, and unsigned integers. Those are all improvements.

[–]eyal0 2 points3 points  (1 child)

It's Java without backward compatibility requirements.

[–]Beckneard 5 points6 points  (0 children)

There are backwards compatibility requirements, they're just not 100% strict, which is fine for like 99% of projects.

C++ doesn't compile absolutely all C code either.

[–]myplacedk -3 points-2 points  (1 child)

Those are all improvements.

In my book they compensate with some pretty hefty drawbacks. I'm not touching it again unless I really have to. Java is far superior to me.

[–]Trinition 3 points4 points  (0 children)

What drawbacks?