dimanche 4 juillet 2021

How to refactor the java code which has if-else statement

I wonder if has any good technic can make the code below more clean.

final Optional<Long> historyHours1;
final Optional<Double> historyHours2;
final Optional<Float> historyHours3;

if (parameters.isPresent()) {
  historyHours1 = // some logic
  historyHours2 = // some logic
  historyHours3 = // some logic
} else {
  historyHours1 = Optional.empty();
  historyHours2 = Optional.empty();
  historyHours3 = Optional.empty();
}

want to keep the variables as final, and use some technic to replace the if-else. considered to use a method to do the calculation and return an object which contains the there variables, but to combines the there variables into an object, is not expected.

Aucun commentaire:

Enregistrer un commentaire