10

I've recently seen a blog post (tweeted by @java) that suggests the following code is becoming increasingly common:

Optional.ofNullable(i).ifPresent(x -> doBlah(x));

instead of:

if (i != null) {
  doBlah(i);
}

The use of Optional in this case appears very awkward to me, even ignoring the naming of the variables - the latter is easier to read and more idiomatic for the use case (handling nulls). I believe this also captures semantics better - i is likely from code that doesn't adhere to the semantics that Optional is trying to capture (as described in the possible duplicate and in this Oracle article).

I do not see one, but is there a good semantic cause to prefer the Optional.isNullable approach (ignoring the performance impact it may have depending on how it is used)?

James Fry
  • 1,133
  • 1
  • 11
  • 28
  • 1
    `orElse(x->doBla(x))` is incorrect, unless `i` happens to be a lambda. – shmosel Feb 16 '17 at 21:59
  • 1
    http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html – shmosel Feb 16 '17 at 22:00
  • 1
    http://stackoverflow.com/documentation/java/152/optional – shmosel Feb 16 '17 at 22:01
  • 1
    Yeah, for the case you cited I'd just stick with a good old `if` statement. But you can do some [neat tricks with filtering and mapping optionals](http://codingjunkie.net/working-with-java8-optionals/) – dnault Feb 16 '17 at 22:03
  • 1
    Those two pieces of code you gave are logically different. The first looks like it is trying to call `doBla` if `i` is null (though it doesn't look right to me). The second calls `doBla` if `i` is not null. – khelwood Feb 16 '17 at 22:03
  • 1
    It should say `ifPresent`, not `orElse`. Otherwise it doesn't make any sense. – khelwood Feb 16 '17 at 22:11
  • I blindly copied the source; I'll update with correct examples and reword slightly to better capture my query. – James Fry Feb 16 '17 at 22:14
  • 1
    As interesting as this question is, I think this question is off-topic for Stack Overflow as it is an opinion-based question. – Joe C Feb 16 '17 at 22:26
  • 4
    IMO: Optional **should not** be used this way. If it's becoming more common, well, if all your friends jump off a bridge... – erickson Feb 16 '17 at 22:37
  • 1
    @JoeC I don't think the question is opinion-based because the example given is not how Optionals were actually intended to be used. – Kylos Feb 16 '17 at 22:43

1 Answers1

25

It doesn't make much sense for the same unit of code to wrap a potentially null object in an Optional only to call ifPresent() on it.

The more useful case is for an API that can return null objects to instead return an Optional. This forces the caller to handle potential null results in a null-safe way. Since the API and the caller are separate units of code, the extra work of wrapping an object in an Optional and forcing the caller to invoke ifPresent() is not just busy-work but actually enforces a safer contract that protects against null-pointer exceptions.

Kylos
  • 1,888
  • 14
  • 24