mercredi 12 décembre 2018

how to get rid of checks for null and if-else if signature in this code?

I get complex object response. It look like this:

    class ComplexObject{
        private PartOne partOne;
        private PartTwo partTwo;
        private PartThree partThree;
    }

And I need process this response. Now I do it like this:

    if (partOne!= null) {
        processOne(partOne);
    } else if (partTwo != null) {
        processTwo(partTwo);
    } else if (partThree != null) {
        processThree(partThree);
    }

But it looks bad. If I could influence the external service, I would add enum Status.PART_ONE, Status.PART_TWO, Status.PART_THREE but I can't do it.

How do I rewrite this code to make it cleaner?

Aucun commentaire:

Enregistrer un commentaire