Quantcast
Viewing all articles
Browse latest Browse all 29

Answer 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> op2) {    ProtoRequest.Builder builder = ProtoRequest.newBuilder();    op1.ifPresent(builder::setOp1);    op2.ifPresent(builder::setOp2);...}

I think in such cases it might be useful to have optional as parameters. API receiving the proto request would handle the different fields.If a function is not doing additional computations on these parameters then using Optional might be simpler.

public void setParameters(A op1, B op2) {    ProtoRequest.Builder builder = ProtoRequest.newBuilder();    if (op1 != null) {        builder.setOp1(op1);    }    if (op2 != null) {        builder.setOp2(op2);    }...}

Viewing all articles
Browse latest Browse all 29

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>