Answer by Jean Spector for Why should Java 8's Optional not be used in arguments
I noticed that you mentioned a a number of approaches, and during the years since the question was asked, one alternative wasn't really explored - the Builder design pattern.The idea is that with a...
View ArticleAnswer by Chris Happy for Why should Java 8's Optional not be used in arguments
If wanting an optional primitive type, just use the primitive's class.For those coming from a C++ background and got confused.E.g. for an optional boolean, use Boolean.The biggest problem is that...
View ArticleAnswer by Mike Nakis for Why should Java 8's Optional not be used in arguments
So, if you would permit the pun, Oracle issued an oracle:Thou shalt not use Optional but for function return values.Ηere is a dissenting opinion:You can completely eliminate null from your code base by...
View ArticleAnswer by Whimusical for Why should Java 8's Optional not be used in arguments
A good example were Optional as arguments would be nice is JPA Repositories. Id love to do something like findByNameAndSurname(Optional,Optional). That way, if the Optional is empty, no WHERE param=y...
View ArticleAnswer by Ali for Why should Java 8's Optional not be used in arguments
Irrespective of Java 8, use old-school method overloading to bring clarity and flexibility.Suppose you have the following method with two arguments:public void doSomething(arg1, arg2);If you want to...
View ArticleAnswer by low_key for Why should Java 8's Optional not be used in arguments
Using Optional as parameters might be useful in some use cases which involves protobufs or setting fields in a configuration object.public void setParameters(Optional<A> op1, Optional<B>...
View ArticleAnswer by Dmitrii Semikin for Why should Java 8's Optional not be used in...
Maybe I will provoke a bunch of down-votes and negative comments, but... I cannot stand.Disclaimer: what I write below is not really an answer to the original question, but rather my thoughts on the...
View ArticleAnswer by drew for Why should Java 8's Optional not be used in arguments
Let's make something perfectly clear: in other languages, there is no general recommendation against the use of a Maybe type as a field type, a constructor parameter type, a method parameter type, or a...
View ArticleAnswer by Xiaolong for Why should Java 8's Optional not be used in arguments
Check out the JavaDoc in JDK10, https://docs.oracle.com/javase/10/docs/api/java/util/Optional.html, an API note is added:API Note: Optional is primarily intended for use as a method return type where...
View ArticleAnswer by MiguelMunoz for Why should Java 8's Optional not be used in arguments
First of all, if you're using method 3, you can replace those last 14 lines of code with this:int result = myObject.calculateSomething(p1.orElse(null), p2.orElse(null));The four variations you wrote...
View ArticleAnswer by speedogoo for Why should Java 8's Optional not be used in arguments
This is because we have different requirements to an API user and an API developer. A developer is responsible for providing a precise specification and a correct implementation. Therefore if the...
View ArticleAnswer by gpilotino for Why should Java 8's Optional not be used in arguments
Accepting Optional as parameters causes unnecessary wrapping at caller level.For example in the case of:public int calculateSomething(Optional<String> p1, Optional<BigDecimal> p2 {}Suppose...
View ArticleAnswer by Blair for Why should Java 8's Optional not be used in arguments
I know that this question is more about opinion rather than hard facts. But I recently moved from being a .net developer to a java one, so I have only recently joined the Optional party. Also, I'd...
View ArticleAnswer 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...
View ArticleAnswer by Pau for Why should Java 8's Optional not be used in arguments
Another reason to be carefully when pass an Optional as parameter is that a method should do one thing... If you pass an Optional param you could favor do more than one thing, it could be similar to...
View ArticleAnswer by Eddy for Why should Java 8's Optional not be used in arguments
My take is that Optional should be a Monad and these are not conceivable in Java.In functional programming you deal with pure and higher order functions that take and compose their arguments only based...
View ArticleAnswer by Swaraj Yadav for Why should Java 8's Optional not be used in arguments
One more approach, what you can do is // get your optionals firstOptional<String> p1 = otherObject.getP1();Optional<BigInteger> p2 = otherObject.getP2();// bind values to a...
View ArticleAnswer by Gili for Why should Java 8's Optional not be used in arguments
The best post I've seen on the topic was written by Daniel Olszewski:Although it might be tempting to consider Optional for not mandatory method parameters, such a solution pale in comparison with...
View ArticleAnswer by Mark Perry for Why should Java 8's Optional not be used in arguments
There are almost no good reasons for not using Optional as parameters. The arguments against this rely on arguments from authority (see Brian Goetz - his argument is we can't enforce non null...
View ArticleAnswer by Kieran for Why should Java 8's Optional not be used in arguments
Optionals aren't designed for this purpose, as explained nicely by Brian Goetz.You can always use @Nullable to denote that a method argument can be null. Using an optional does not really enable you to...
View Article