Reducing complexity of code for if-else statements

Angelina :

My code analysis plugin is complaining about code complexity in the method that contains following code. I noticed following code looks like it could be combined, but I am not sure how to do it:

for(Command command : commands) {
    if (command instanceof AddCommand || command instanceof UpdateCommand) {
        if (!isMaturityDateInPast() && !paymentDueDate().isAfter(LocalDate.now())) {
            command.execute(request);
        }
    } else {
        command.execute(request);
    }
}

I tried introducing boolean variable and setting it in both if and else statements, but that just adds more lines of code. I am not very good when it comes to logically placing parts of code that have something in common. I can tell this if-else could be combined, but I don't know how to do it. Can someone shed some light?

Fallen :

I would do early continue to avoid the repetition of command.execute(). I don't think it's worth to combine the conditions in one or creating another function for its sake.

for(Command command : commands) {
  if (command instanceof AddCommand || command instanceof UpdateCommand) {
    if (isMaturityDateInPast() || paymentDueDate().isAfter(LocalDate.now())) {
        continue;
    }
  }
  command.execute();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=127200&siteId=1