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 ArticleAnswer by Steve B. for Why should Java 8's Optional not be used in arguments
This seems a bit silly to me, but the only reason I can think of is that object arguments in method parameters already are optional in a way - they can be null. Therefore forcing someone to take an...
View ArticleAnswer 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...
View ArticleAnswer by Joop Eggen for Why should Java 8's Optional not be used in arguments
Oh, those coding styles are to be taken with a bit of salt.(+) Passing an Optional result to another method, without any semantic analysis; leaving that to the method, is quite alright.(-) Using...
View ArticleAnswer by Makoto for Why should Java 8's Optional not be used in arguments
The pattern with Optional is for one to avoid returningnull. It's still perfectly possible to pass in null to a method.While these aren't really official yet, you can use JSR-308 style annotations to...
View ArticleAnswer by Macchiatow for Why should Java 8's Optional not be used in arguments
I believe the reson of being is you have to first check whether or not Optional is null itself and then try to evaluate value it wraps. Too many unnecessary validations.
View ArticleAnswer by Danil Gaponov for Why should Java 8's Optional not be used in...
I think that is because you usually write your functions to manipulate data, and then lift it to Optional using map and similar functions. This adds the default Optional behavior to it.Of course, there...
View ArticleWhy should Java 8's Optional not be used in arguments
I've read on many Web sites Optional should be used as a return type only, and not used in method arguments. I'm struggling to find a logical reason why. For example I have a piece of logic which has 2...
View ArticleAnswer by Marko for Why should Java 8's Optional not be used in arguments
In case you want to have an Optional argument in your function rather do it using another functional interface. For example:void doSomething(Supplier<Optional<String>> urlSupplier) {...
View ArticleAnswer by Eliezer Garza for Why should Java 8's Optional not be used in...
after a lot of debate at my new posting we came with this conventionfor public methods:a primitive means not null (obvious)an Object Backed by a primitive means it's nullableeven if it's not a...
View Article