patternjavaCritical
Why should Java 8's Optional not be used in arguments
Viewed 0 times
javawhyshouldusedoptionalnotarguments
Problem
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 optional parameters. Therefore I think it would make sense to write my method signature like this (solution 1):
Many web pages specify Optional should not be used as method arguments. With this in mind, I could use the following method signature and add a clear Javadoc comment to specify that the arguments may be null, hoping future maintainers will read the Javadoc and therefore always carry out null checks prior to using the arguments (solution 2):
Alternatively I could replace my method with four public methods to provide a nicer interface and make it more obvious p1 and p2 are optional (solution 3):
Now I try writing the code of the class which invokes this piece of logic for each approach. I first retrieve the two input parameters from another object which returns
if solution 2 is used, the calling code would look like this:
if solution 3 is applied, I could use the cod
public int calculateSomething(Optional p1, Optional p2) {
// my logic
}Many web pages specify Optional should not be used as method arguments. With this in mind, I could use the following method signature and add a clear Javadoc comment to specify that the arguments may be null, hoping future maintainers will read the Javadoc and therefore always carry out null checks prior to using the arguments (solution 2):
public int calculateSomething(String p1, BigDecimal p2) {
// my logic
}Alternatively I could replace my method with four public methods to provide a nicer interface and make it more obvious p1 and p2 are optional (solution 3):
public int calculateSomething() {
calculateSomething(null, null);
}
public int calculateSomething(String p1) {
calculateSomething(p1, null);
}
public int calculateSomething(BigDecimal p2) {
calculateSomething(null, p2);
}
public int calculateSomething(String p1, BigDecimal p2) {
// my logic
}Now I try writing the code of the class which invokes this piece of logic for each approach. I first retrieve the two input parameters from another object which returns
Optionals and then, I invoke calculateSomething. Therefore, if solution 1 is used the calling code would look like this:Optional p1 = otherObject.getP1();
Optional p2 = otherObject.getP2();
int result = myObject.calculateSomething(p1, p2);if solution 2 is used, the calling code would look like this:
Optional p1 = otherObject.getP1();
Optional p2 = otherObject.getP2();
int result = myObject.calculateSomething(p1.orElse(null), p2.orElse(null));if solution 3 is applied, I could use the cod
Solution
Oh, those coding styles are to be taken with a bit of salt.
In general: Optional unifies two states, which have to be unraveled. Hence better suited for result than input, for the complexity of the data flow.
- (+) Passing an Optional result to another method, without any semantic analysis; leaving that to the method, is quite alright.
- (-) Using Optional parameters causing conditional logic inside the methods is literally contra-productive.
- (-) Needing to pack an argument in an Optional, is suboptimal for the compiler, and does an unnecessary wrapping.
- (-) In comparison to nullable parameters Optional is more costly.
- (-) The risk of someone passing the Optional as null in actual parameters.
In general: Optional unifies two states, which have to be unraveled. Hence better suited for result than input, for the complexity of the data flow.
Context
Stack Overflow Q#31922866, score: 375
Revisions (0)
No revisions yet.