Refactor method by removing else condition Given the below code is there a way to eliminate the else clause from the below method?
In my workplace else clause is considered evil because it somehow decreases the readability of the code.
public boolean validateAndSetDateRelatedSessionAttributes(String startDateFromParam, String endDateFromParam, HttpSession session) throws ParseException {
    boolean isDateRangeValid = true;
    final boolean requestContainsCustomDate = requestContainsCustomDate(startDateFromParam, endDateFromParam);

    if(requestContainsCustomDate || sessionContainsCustomDate(session)) {
        if(requestContainsCustomDate) {
            if(!isDateRangeValid(startDateFromParam, endDateFromParam)) {
                isDateRangeValid = false;
                startDateFromParam = session.getAttribute(BACalendarConstant.START_DATE).toString();
                endDateFromParam = session.getAttribute(BACalendarConstant.END_DATE).toString();
            }
        } else {
            startDateFromParam = session.getAttribute(BACalendarConstant.START_DATE).toString();
            endDateFromParam = session.getAttribute(BACalendarConstant.END_DATE).toString();
        }
        setSessionAttributes(startDateFromParam, endDateFromParam, session);
    }
    return isDateRangeValid;
}
&quot;Else is evil&quot;? That's a very peculiar point of view!
But if you really want to avoid it, you can look at early return, provided that isn't also regarded as evil - some people are very firm on the &quot;single entry, single exit&quot; principle.
Gilbert Le Blanc's code takes a similar approach to me, but in avoiding early return, he risks duplicated code execution, which may not be a problem here, but worries me a little.
The code below is not checked in any detail, but shows the idea, though it does duplicate code.
public boolean validateAndSetDateRelatedSessionAttributes(String startDateFromParam, String endDateFromParam, HttpSession session) throws ParseException {

  if (requestContainsCustomDate(startDateFromParam, endDateFromParam)) {
    boolean isDateRangeValid = isDateRangeValid(startDateFromParam, endDateFromParam);
    if (!isDateRangeValid) {
      startDateFromParam = session.getAttribute(BACalendarConstant.START_DATE).toString();
      endDateFromParam = session.getAttribute(BACalendarConstant.END_DATE).toString();
    }
    setSessionAttributes(startDateFromParam, endDateFromParam, session);
    return isDateRangeValid;
  }

  if (sessionContainsCustomDate(session)) {
    startDateFromParam = session.getAttribute(BACalendarConstant.START_DATE).toString();
    endDateFromParam = session.getAttribute(BACalendarConstant.END_DATE).toString();
    setSessionAttributes(startDateFromParam, endDateFromParam, session);
  }

  return true;
}