jeudi 29 octobre 2020

How to avoid multiple if statements where result is add to list

I would like to ask you for an idea. I have class, where I have a lot of if statements, lets say it looks like that:

List<String> errors = new ArrayList<>();
boolean errorCodition1 = SuperUtils.calculateIfObjectIsCorrect(this); // rest of them same

if(errorCondition1){
    errors.add(errorsMessage.getSuperError());
}

if(errorCondition2){
    errors.add(errorsMessage.getMinorError());
}

if(errorCondition3){
    errors.add(errorsMessage.getAlmostCriticalError());
}

if(errorCondition4){
    errors.add(errorsMessage.getOverNineThousendError());
}

displayErrors(errors);

Maybe this code does not look super terrible, but lets assume that we will need to add more conditions. How would you avoid it?

This code is called always when class is initialize, so lets say for example in constructor.

Aucun commentaire:

Enregistrer un commentaire