Answer 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Article