Question: We are persisting an object in the database, But before there are few conditions that need to be checked I am using if-else to perform these what is the best way to eliminate if-else statements. We are also setting response object which a map in each and every statement.

Also, in future we have to check for other conditions before creating or updating.

```
Callable<Response> callableObj = () -> {
    if(isNew){
    if(checkCreatePermission()){
       if(checkDuplicate()) {
          return foundObejct();
       } else {
           persistNewObject();
       }
    } else {
        throw new Exception();
    }
} else {
    if(checkUpdatePermission()){
        udapteObject();
    } else {
        throw new Exception();
    }

}
};

```

Answer: 
You could use some early returns/throws. Makes the code a little more readable.

```
if(isNew){
    if(!checkCreatePermission()){
        throw new Exception();
    }
    if (checkDuplicate()) {
       return foundObejct();
    }
    persistNewObject();
} else {
    if(!checkUpdatePermission()){
        throw new Exception();
    }
    udapteObject();

}

```
You can get inspired from the [CoR design pattern](https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern). In the code below I assume that to run a `Callable` you call its `apply()` method:

```
Callable<Response> cError = () -> { throw new Exception(); };

Callable<Response> cUpdateObject = () ->
    !isNew && checkUpdatePermission() ? updateObject() : cError.apply();

Callable<Response> cPersistNewObject = () ->
    isNew && checkCreatePermission() && !checkDuplicate() ? persistNewObject() : cUpdateObject.apply();

Callable<Response> callableObj = () ->
    isNew && checkCreatePermission() && checkDuplicate() ? foundObject() : cPersistNewObject.apply();

```

With this approach, it's easy to see the conditions leading to each of the responses.

P/S: You can cache the calls to `checkCreatePermission()`, `checkUpdatePermission()`, and `checkDuplicate()` if necessary.