mercredi 28 août 2019

refactoring nested if statements

I'm trying to refactor some code that looks similar to the code below. I feel that there should be a simpler way to do what it does. My main concern is the call to getDataB() in both if and else blocks of the (!data.isEmpty()) condition.

    DataA dataA = getDataA();

    if (!dataA.isEmpty()) {

        if (dataA == Status.FINAL) {
            // process finalized dataA
        }

        if (dataA == Status.RECEIVED) {
            DataB dataB = getDataB();
            if (!dataB.isEmpty()) {
                dataA.setStatus(dataB.getStatus());
                // process dataA with updated status from dataB
            }
        }

        if (dataA == STATUS.IN_PROGRESS) {
            // process in progress dataA
        }

    // empty dataA
    } else {
        Datab dataB = getDataB();
        if (!dataB.isEmpty()) {
            // process dataB
        }
    }

Aucun commentaire:

Enregistrer un commentaire