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: 
> 
> Best way to eliminate many if-else conditions...
> 
> 
> 

One way to better code is to invert the if conditions and then factor the conditional code out into their separate methods.

The code below is functionally equivalent to the code you have written.

```
public ... originalMethod() {
    if(isNew) handleNewObject();
    else handleExistingObject();
}

private ... handleExistingObject() {
    if (!checkUpdatePermission()) throw new Exception();
    updateObject();
}

private ... handleNewObject() {
    if (!checkCreatePermission()) throw new Exception();
    if (checkDuplicate()) return foundObject();
    persistNewObject();
    //... other steps.
}

```
I would suggest to split it down in few methods. Not sure if I got what do you need, but it could be something like

```java
    createOrUpdate() {
        if(isNew()){
            create();
        } else {
            update();
        }
    }

    create() {
        if(checkCreatePermission()){
            if(checkDuplicate()) {
                return foundObejct();
            } else {
                persistNewObject();
            }
        } else {
            throw new Exception();
        }
    }

    update() {
        if(checkCreatePermission()){
            udapteObject();
        } else {
            throw new Exception();
        }
    }

```