Quantcast
Viewing all articles
Browse latest Browse all 29

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

At first, I also preferred to pass Optionals as parameter, but if you switch from an API-Designer perspective to a API-User perspective, you see the disadvantages.

For your example, where each parameter is optional, I would suggest to change the calculation method into an own class like follows:

Optional<String> p1 = otherObject.getP1();Optional<BigInteger> p2 = otherObject.getP2();MyCalculator mc = new MyCalculator();p1.map(mc::setP1);p2.map(mc::setP2);int result = mc.calculate();

Viewing all articles
Browse latest Browse all 29

Trending Articles



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