Is it OK to pass Optional as a parameter to Java methods?
As for me it's bad because after passing Optional to the method, we create situation when method should do two things:
- Some logic when object is present
- Another logic that covers case when object is absent
For example:
public class Example {
private String receiveSomeString(Optional<SomeReceiver> printer) {
return printer
.map(SomeReceiver::getA)
.orElse("B");
}
}
class SomeReceiver {
public String getA(){
return "A";
};
}
As for me it's break Single Responsibility Principle (SRP) and method should do two things.
Is it correct?