lundi 11 février 2019

Elegant way to replace multiple if statemens to throw exceptions

my application has a service where 3 methods perform some validation and depending on the returned result a different exception is thrown.

  if (!check1stCondition() {
    throw new ValidationException(message1);
  }

  if (!check2ndCondition)
    throw new ValidationException(message2);

  if (!check3rdCondition)
    throw new ValidationException(message3);
}

How could I reformat this code in order to be maintaneable in the future? In the future new checks could be performed.

Aucun commentaire:

Enregistrer un commentaire