samedi 7 septembre 2019

Code refactoring technique to encapsulate and simplify validation logic for pojo

We have a Sprint object and we have to perform some validation in our code and we have written some private helper methods like this:

    private boolean sprintDateEqualsIntervalStartOrEndDate(Sprint sprint, LocalDate startDate, LocalDate endDate)
  {
    return sprint.getStartDate().isEqual(startDate) || sprint.getStartDate().isEqual(endDate) || sprint.getEndDate().isEqual(startDate) || sprint.getEndDate().isEqual(endDate);
  }

  private boolean sprintIsInBetweenTheIntervalDates(Sprint sprint, LocalDate startDate, LocalDate endDate)
  {
    return sprint.getStartDate().isAfter(startDate) && sprint.getEndDate().isBefore(endDate);
  }

  private boolean sprintStartDateOrEndDateIsInInterval(Sprint sprint, LocalDate startDate, LocalDate endDate)
  {
    return (sprint.getEndDate().isBefore(endDate) && sprint.getEndDate().isAfter(startDate) && sprint.getStartDate().isBefore(startDate)) ||
            (sprint.getStartDate().isBefore(endDate) && sprint.getStartDate().isAfter(startDate) && sprint.getEndDate().isAfter(endDate));
  }

Now as you can see there are too many validations. One approach we thought is to move some of the code inside the POJO (Sprint class) itself. Is it a good practice. I have seen code where they say pojo.someMethodOtherThenSetterGetter(...) e.t.c.

Or what do you guys recommend?

Aucun commentaire:

Enregistrer un commentaire