Hello i have two samples of code
if/else if/else statements
private Object getObj(message) {
if (message.getA() != null)
return message.getA();
else if (message.getB() != null)
return message.getB();
else if (message.getC() != null)
return message.getC();
else return null;
}
Optional statements
private Optional<Object> wrap(Object o){
return Optional.ofNullable(o);
}
private Object getObj(message) {
return wrap(message.getA())
.orElseGet(() -> wrap(message.getB())
.orElseGet(() -> wrap(message.getC())
.orElse(null)));
}
So my question is how these two compare in terms of performance (i have about 15-20 if-else statements on actual code)?
Is it worth refactoring the code readability vs performance or is a misusage of optionals?
Also what is the performance penalty in case the if/else-if statements grown to 100+?
Thanks in advance
Aucun commentaire:
Enregistrer un commentaire