News for hackers. Stuff that matters.
Re:Java 8’s new Optional type doesn't solve anything by jcrites on Monday October 26, 2015 @06:02AM (#10450385)
The lack of pattern matching doesn't make it useless. The existence of Optional reminds the programmer to check whether the value is present, and the type system does enforce this; you can't accidentally treat an Optional as a reference of the same type. The type system does help us remember to handle things; it is a reminder enforced by the type system that's easy to examine during code review.

Yes, you can write get() and risk an exception. So don't do that. I'm still glad get() is present because there are times when the human knows the value will always be present, but sometimes in a way that can't be encoded in the type system. Alternatively, sometimes an absent value is an error and get() is an acceptable way to check-and-throw.

I agree with the suggestion that static analysis can be useful, though many users had that already with FindBugs or Fortify. If we're talking about annotations based analysis, then it only goes so far. Optional and streams provide useful new tools for documenting intentions from my perspective. Another reason I enjoy the type is because it makes it a little more reasonable to say that, at least in new code, all references are nonnull references by default, since they'd be Optional otherwise.

Re:Java 8’s new Optional type doesn't solve anything by bkv on Monday October 26, 2015 @09:43AM (#10451324)
>Yes, you can write get() and risk an exception. So don't do that.

Yes, you can dereference a null pointer and risk an exception. So don't do that.

See where this is going?

The problem with Java's optional is that it is by all appearances (in name and high-level description), a general purpose optional, albeit with a ton of gotchas that will result in people saying "Just don't do that."

Java's optional is not a general purpose option type. In fact, its intended use-case is very specific, according to a rather obscure SO answer by Brian Goetz: http://stackoverflow.com/a/26328555/547365

This is, of course, just tribal knowledge, meaning people are going to be doing shit with optional that they shouldn't, and it's going to be messy.

Re:Java 8’s new Optional type doesn't solve anything by peeters on Monday October 26, 2015 @11:35AM (#10452094)
>>Yes, you can write get() and risk an exception. So don't do that.

>Yes, you can dereference a null pointer and risk an exception. So don't do that.

Except these aren't at all the same. With a reference, you have no good way of telling whether your reference IS nullable. With Optional, it's ALWAYS optional.

Therefore it is trivial to a) train developer habits to always check before fetch, and b) enforce an isPresent check with static checks.

Sure pattern decomposition is nicer, but that doesn't mean that Java's implementation "defeats the purpose entirely" like the author suggests. I'd love everything to be checked statically, but if that can't be done, I still like strong conventions which make the code clearer and safer.

Re:Java 8’s new Optional type doesn't solve anything by bkv on Monday October 26, 2015 @12:09PM (#10452311)
>Except these aren't at all the same. With a reference, you have no good way of telling whether your reference IS nullable.

As the article mentions, we have annotations to do just that.

>Therefore it is trivial to a) train developer habits to always check before fetch, and b) enforce an isPresent check with static checks.

Again, if we're using static analysis to verify the correct use of Optional, why are we using optional at all? Why wouldn't we use static checks to verify null references can't be dereferenced, instead of hypothetical yet-to-be-implemented static checks that may not be implemented anyway? See https://github.com/findbugsproject/findbugs/issues/56

Re:Java 8’s new Optional type doesn't solve anything by peeters on Monday October 26, 2015 @12:58PM (#10452667)
>>Except these aren't at all the same. With a reference, you have no good way of telling whether your reference IS nullable.

>As the article mentions, we have annotations to do just that.

Your mileage may vary, but my experience with nullable/nonnull annotations is that because they are not used across the board, you are either left with a ton of false positives or a ton of false negatives. The fact that there hasn't really been a great standardization in the core language is probably evidence of that (yes, JSR305 exists, but the JDK doesn't enforce its constraints by default).

With Optional, it's a clean slate. You can simply say "a reference to an Optional shall never be null" and you could probably enable such an enforcement for an existing project. Of course it's not going to be 100% while it's still a normal reference type, but catching 99% of the cases is a really big improvement!

Re:Java 8’s new Optional type doesn't solve anything by mike_hearn on Monday October 26, 2015 @12:33PM (#10452474)
Much better than using static analysis is to have it in the type system:

https://kotlinlang.org/docs/reference/null-safety.html

It's simple:

val x: Foo = maybeGetSomeFoo() // Error: method returns Foo?

val x: Foo? = maybeGetSomeFoo()

x.sayHello() // Error: x may be null

x!!.sayHello() // OK, the !! method throws an exception if it's null

if (x != null) x.sayHello() // OK, flow sensitive typing means the test narrows the type

val h = x?.sayHello() // OK, ?. yields null if left hand side is null, otherwise evaluates right

val h = x?.sayHello() ?: return // OK, ?: runs right hand side only if left hand side is null

Re:Java 8’s new Optional type doesn't solve anything by dllthomas on Tuesday October 27, 2015 @09:06PM (#10462300)
"Much better than using static analysis is to have it in the type system"

I am not sure there is a distinction. Though it is true that some analyses may be run by a broader swath of users.

Re:Java 8’s new Optional type doesn't solve anything by jcrites on Monday October 26, 2015 @01:44PM (#10452968)
No, annotations do not accomplish the same thing in practice. Annotations function properly only when the code was written with annotations, and where the code and all callers are checked against the annotations. Unfortunately, annotations miss the mark in some practical cases:

1) A reference is @NonNull in my code, and I pass it to a component that accepts a regular reference as input. The annotation semantic is simply lost, and I may not even realize it as I'm writing that code. By comparison, if I need to pass an Optional<T> reference where a T reference is needed, then the type system reminds me to consider the present and absent cases. Optional does not allow the programmer to forget.

2) The fact that a component or library was written with annotations does not mean that all usage of the library will properly check the annotations. Sometimes people forget, or sometimes they interact with a component in a dynamic way like through Spring and the semantics get lost. An Optional makes this impossible to forget. You can confuse a @Nullable T with a T, or forget to check upon conversion, but there is no failure case that causes an Optional<T> to turn into a T without handling the present and absent cases.

More choice is better than less choice. You could design an Optional type to omit the get() method. That's less useful than what we have now. Like goto, it has rare but legitimate uses, and it's easy to avoid for the "right way" in the average case (don't use it, flag it during code review, and document the reason why when it's being used).

Re:Java 8’s new Optional type doesn't solve anything by datalist on Monday October 26, 2015 @12:37PM (#10452509)
> With a reference, you have no good way of telling whether your reference IS nullable.

You do, a reference is ALWAYS nullable/optional. Just because people might expect a certain reference never to be null does not change that. You could equally as well as semantically expect an Optional never to be "optional" (it is just a matter of perspective). People who are too lazy to check for null might be too lazy to check isPresent() and both will give you, unsurprisingly, an exception.

java.util.Optional is really just yet another layer added to support laziness in software developers. To put it bluntly, write better manuals/JavaDocs if necessary and, most importantly, read them.

Re:Java 8’s new Optional type doesn't solve anything by peeters on Monday October 26, 2015 @01:03PM (#10452707)
If you ever have actual success getting developers to obey documented input/output constraints without actually enforcing them with a static or runtime check, you could probably become a highly-paid coach. But I'm skeptical. I've never seen it happen, and most good libraries I've used (e.g. Guava) always follow up preconditions with a runtime check. Statically enforceable pre/postconditions shouldn't even have to be documented because the signature is the documentation.

Good libraries allow their developers to be lazy.

Re:Java 8’s new Optional type doesn't solve anything by jontro on Monday October 26, 2015 @06:24AM (#10450450)
Exactly, it forces the programmer to think about whether something can be null or not and actually had a behavioural change in my java coding.
Re:Java 8’s new Optional type doesn't solve anything by Retra on Monday October 26, 2015 @12:02PM (#10452261)
Every reference in Java can be null. You should be thinking about it all the time.

It's the Java language that forces you to think about nulls, not Optional.

Re:Java 8’s new Optional type doesn't solve anything by MereInterest on Monday October 26, 2015 @08:42AM (#10450977)
Wouldn't it be better to have a non-nullable type, akin to C++'s return by value? That way, instead of forcing the programmer to think, you are removing the problem that they would need to think about.
Re:Java 8’s new Optional type doesn't solve anything by hibikir on Monday October 26, 2015 @10:29AM (#10451646)
In other languages that use Option types more natively, like Scala, anything that isn't optional is assumed to just not be null: The compiler doesn't guarantee it (it's not as if it can, we are dealing with a JVM here) but it works in practice. I've not seen a NullPointerException in years.

Option has other advantages, like its compositional qualities (thanks to Option being a monad). So imagine the following code

def foo(x:Option[String]) = x.filter(_.length<10).map(_ + "More data")

If the function returns None if it receives either None or a string longer than 10 characters, and if we got Some("short "), foo will return Some("short More data"). The alternatives without an option type, involve at least a couple of branching statements, even if you have a non nullable type, to convert from nullable to not nullable, while Option does it all for us.

Re:Java 8’s new Optional type doesn't solve anything by the_af on Monday October 26, 2015 @11:34AM (#10452085)
> I've not seen a NullPointerException in years [in Scala]

This sounds odd. Does your code never interface with Java libraries and frameworks? I envy you! It's all too easy to forget to wrap a suspect value from Java-land with an Option(..). And cumbersome even if you do remember.

And then, there's the code written by Scala novices who just love using nulls. Yes, yes, "code reviews", we all know they are always practiced all the time :/

Re:Java 8’s new Optional type doesn't solve anything by mark242 on Monday October 26, 2015 @12:35PM (#10452496)
The Scala landscape is very, very quickly creating their own libraries for similar popular Java libraries/frameworks. It is entirely possible to create an application, web or otherwise, that uses no Java libraries at all (save for the sbt dependency tree).

As for "Scala novices who just love using nulls", if you're seeing that, then the coder in question missed day 1 of Scala training, which is always "if you are writing the word null, you are doing something wrong" (right up there with "if you are writing the word var, you are probably doing something wrong").

Re:Java 8’s new Optional type doesn't solve anything by the_af on Tuesday October 27, 2015 @09:04AM (#10457694)
Maybe, but if true that's still in the future. The OP claimed they hadn't seen a NPE "in years", which simply doesn't match my own experience.

As for novices: yes, novices tend to do that. They tend to miss, misremember or fail to put into practice what they should have learned on day 1. I've also seen senior devs who were not exactly newbies (in general) still using nulls because they don't grok Scala; the famous "you can program Fortran in any language". The reality of software development is that you have to deal with all these people.

Re:Java 8’s new Optional type doesn't solve anything by louthy on Monday October 26, 2015 @10:35AM (#10451696)
Whilst non-nullable references are useful, that isn't what Option is trying to solve. The problem with null references is that when you're handed a reference to an object of type T - the type system is saying to you: "This is the contract I will honour". Except it doesn't when the reference is null.

However the functionality of a null reference type is useful, because it allows a function to return 'no value'. So the goal of an Option type should be to expose the contract for T when there's a value and remove the contract when there's no-value.

The programmer is then explicitly forced to acknowledge the two states that the value could be.

Shameless plug - My C# implementation of Option which is possibly the most complete example out there (at least in C# world):

https://github.com/louthy/language-ext/blob/master/LanguageE...

Re:Java 8’s new Optional type doesn't solve anything by Sharlin on Monday October 26, 2015 @09:40AM (#10451295)
Or, indeed, C++'s references which are non-nullable.
Re:Java 8’s new Optional type doesn't solve anything by pandler on Monday October 26, 2015 @07:16AM (#10450641)
> Exactly, it forces the programmer to think

It's a curious feature, the way you've described it. I have virtually no experience with Java so I'm looking at this from a totally foreign perspective. The authors considers it an ineffective feature because you still have to think about null checks, whereas you consider it an effective feature because it forces you to think about the problem instead of ignoring it.

Re:Java 8’s new Optional type doesn't solve anything by acaloiar on Monday October 26, 2015 @08:52AM (#10451036)
I believe the point @jontro is making is that unsafe programming behavior is unsafe without regard to the language's constraints. Option simply makes it more difficult to behave unsafely. Of course a developer can still call .get() when the presence of a value is unknown, but the Option type lets them know they are being careless in doing so.
Re:Java 8’s new Optional type doesn't solve anything by the_af on Monday October 26, 2015 @11:39AM (#10452115)
No, the author considers it ineffective because you are not forced to think about nulls at compile time. Of course, at run time they will explode in your face, so you still have to consider them eventually.

@jontro is saying that proper Optionals make it impossible (or at least, more difficult) to forget about the problem at compile time.

Re:Java 8’s new Optional type doesn't solve anything by Zardoz84 on Monday October 26, 2015 @08:09AM (#10450818)
I remember that somebody try to defend Java because have references instead of pointers so not is necessary to be worried about null pointers .. Ja!
Re:Java 8’s new Optional type doesn't solve anything by rickette on Monday October 26, 2015 @11:45AM (#10452160)
Java 8 Optional is useful (although the API could be better).

If only FindBugs would warn about calling get() without checking isPresent() first. There's an open FindBugs ticket for this: http://sourceforge.net/p/findbugs/feature-requests/302/

Re:Java 8’s new Optional type doesn't solve anything by bkv on Monday October 26, 2015 @12:07PM (#10452294)
That is the old bug tracker. An issue was submitted on the official tracker and immediately closed. See https://github.com/findbugsproject/findbugs/issues/56 for explanation.