-3

Is it a better approach than the much easier to read if (x==null) then doSomething? Is yes, why?

Both work as expected but unclear on which is the better programming practice and why

Looks like stack overflow needs a minimum of 220 characters so padding with stuff unrelated to my question

Galactus
  • 1
  • 2
  • 1
    Hard to tell what your question is. Are you asking what is the value of using `Optional`s? They force you to add the null check before using it. The 220-char limit is there for a reason. You need to add more details (in actual code) so others can understand your concern. – Ruan Mendes Aug 30 '23 at 14:39
  • With Optional it is more obvious that the value can by null and so it has to be checked – Andreas Radauer Aug 30 '23 at 14:41
  • 2
    Note, in an ideal world, `Optional` would only ever be used as the _return type_ of methods, never as a parameter or field type. So, if you have a non-`Optional` local variable? Prefer `if` over `Optional`. But if you already have an `Optional` from a method call, then obviously prefer the `Optional` API. – Slaw Aug 30 '23 at 14:57
  • No, Optional is **not** a replacement for `if`. Feel free to read [what a Java architect had to say on the subject](https://stackoverflow.com/questions/23454952/uses-for-optional). – VGR Aug 30 '23 at 16:30

0 Answers0