Saturday, 7 September 2013

Should I use Java8/Guava Optional for every method that may return null?

Should I use Java8/Guava Optional for every method that may return null?

Optional is used to represent nullable object, Some uses of this class
include
1 As a method return type, as an alternative to returning null to indicate
that no value was available 2 To distinguish between "unknown" (for
example, not present in a map) and "known to have no value" (present in
the map, with value Optional.absent()) 3 To wrap nullable references for
storage in a collection that does not support null (though there are
several other approaches to this that should be considered first)
For the first case, do I need to return Optional in all nullable return
method?

No comments:

Post a Comment