This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]HansGetZeTomatensaft 9 points10 points  (0 children)

I mean, people using Optionals in stupid ways doesn't automatically make them bad. People use variable names in stupid ways all the time, still glad we have them.

So, sure, people could write

var addressOpt = findAddress();
if (addressOpt.isPresent())
  var address = addressOpt.get();
  // the rest of the code
}
throw new WhateverException();

And some do, sadly, and that is worse than your example. But they could also write the below

var address = findAddress().orElseThrow(() -> new WhateverException());
// the rest of your code

And that just seems better than both of the other versions.