Re:Java 8’s new Optional type doesn't solve anything
by Latty on Monday October 26, 2015 @07:04AM
(#10450604)
This is another off-base point, in my opinion. Sure, it's possible for it to be null, and that isn't great - but the vast majority of null problems come from the user assuming the interface won't give you a null. Something returning an Optional should never return null, so the ambiguity is resolved. Sure, it still could as a bug, but that's actually a pretty rare case. I don't see it being a real problem.
Re:Java 8’s new Optional type doesn't solve anything
by organsnyder on Monday October 26, 2015 @07:15AM
(#10450637)
Exactly. A null Optional reference is such an obvious bug, that it can easily be detected by simple analysis tools. OTOH, a null is often an accepted (if derided) method of indicating that a value simply doesn't exist, so it's much more difficult to detect when a null will cause a problem.
Re:Java 8’s new Optional type doesn't solve anything
by bakhy on Monday October 26, 2015 @07:13AM
(#10450632)
that makes sense. i think an important question here is - what does "return null" in a method which returns Optional<T> do? will it return a null Optional<T>, or will it return a non-null Optional<T> which is empty?
sorry to rub it in, but Java needs value types. this whole issue, however maybe irrelevant, does not exist in C#.
Re:Java 8’s new Optional type doesn't solve anything
by KfZ on Monday October 26, 2015 @10:37AM
(#10451708)
It's a very simple question to answer: "return null" returns null.
Re:Java 8’s new Optional type doesn't solve anything
by bakhy on Monday October 26, 2015 @10:57AM
(#10451862)
then this abstraction has sprung a serious leak. the article seems to be fully right.
Re:Java 8’s new Optional type doesn't solve anything
by Latty on Wednesday October 28, 2015 @11:12AM
(#10464863)
The point is that wherever that happens, it's an obvious flaw with the method returning the null, and can also be caught by static analysis. It means that in general, the problem is avoided.
Yes, it's not ideal, it's still a flaw that should be caught at compile-time, but it's not a big real-world problem, as returning null by accident instead of an option is a pretty rare bug.
The much more common bug (you didn't realize no return value was a case), the Option is a good counter to.
No, it's not a perfect solution, but it's not useless either.
Re:Java 8’s new Optional type doesn't solve anything
by bakhy on Wednesday October 28, 2015 @12:49PM
(#10465617)
i understand, and agree. i would personally, though, avoid adding it, and add attributes for the same purpose instead. just as the article suggests... it achieves the same without introducing a new class, thus also remaining backwards compatible.
"The last flaw with Java's implementation is a bit ironic — It's possible for the optional reference itself to be null."
ouch.