Codeninja
Codeninja

Reputation: 322

How to return boolean "if-then-else" statement in a single return

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;
    }
  }

Upvotes: 0

Views: 1773

Answers (1)

Mazen
Mazen

Reputation: 58

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;

Upvotes: 2

Related Questions