Quantcast
Viewing latest article 22
Browse Latest Browse All 29

Answer by llogiq for Why should Java 8's Optional not be used in arguments

This advice is a variant of the "be as unspecific as possible regarding inputs and as specific as possible regarding outputs" rule of thumb.

Usually if you have a method that takes a plain non-null value, you can map it over the Optional, so the plain version is strictly more unspecific regarding inputs. However there are a bunch of possible reasons why you would want to require an Optional argument nonetheless:

  • you want your function to be used in conjunction with another API that returns an Optional
  • Your function should return something other than an empty Optional if the given value is empty
  • You think Optional is so awesome that whoever uses your API should be required to learn about it ;-)

Viewing latest article 22
Browse Latest Browse All 29

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>