0

I am getting a sonar issue of Replace this if-then-else statement by a single return statement. As I have three conditions in the if condition, how can I return this without if-else?

  public boolean isDateRangeExceedOneYear(CardPluginInterface plugin, Date begin, Date end)
  {
    if (!DateTimeToolkit.getInstance().isEmpty(begin)
        && ((CardValidatorInterface) plugin).isValidateYearDateRangeFor()
        && DateTimeToolkit.getInstance().compare(
        DateTimeToolkit.getInstance().add(new DateTime(begin, new Time((short) 0, (short) 0, (short) 0)), 1, 1).date,
        end) <= 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
cs95
  • 379,657
  • 97
  • 704
  • 746
Codeninja
  • 322
  • 1
  • 4
  • 18
  • Does this answer your question? [Replace this if-then-else statement by a single return statement](https://stackoverflow.com/questions/45817581/replace-this-if-then-else-statement-by-a-single-return-statement) – Ole V.V. Jun 01 '20 at 07:33

1 Answers1

2

For the above you can use

return !DateTimeToolkit.getInstance().isEmpty(begin)
    && ((CardValidatorInterface) plugin).isValidateYearDateRangeFor()
    && DateTimeToolkit.getInstance().compare(
    DateTimeToolkit.getInstance().add(new DateTime(begin, new Time((short) 0, (short) 0, (short) 0)), 1, 1).date,
    end) <= 0;
Mazen
  • 58
  • 9