samedi 19 septembre 2015

How replace If-else block condition

In my code I have an if-else block condition like this:

public String method (Info info) {
    if (info.isSomeBooleanCondition) {
        return "someString";
    }
    else if (info.isSomeOtherCondition) {
        return "someOtherString";
    }
    else if (info.anotherCondition) {
        return "anotherStringAgain";
    }
    else if (lastCondition) {
        return "string ...";
    }
    else return "lastButNotLeastString";
}

Each conditional branch returns a String.

Since if-else statements are difficult to read, test and maintain, how can I replace? I was thinking to use Chain Of Responsability Pattern, is it right in this case? Is there any other elegant way that I can do that?

Aucun commentaire:

Enregistrer un commentaire