mercredi 13 juillet 2016

Refactoring 'if else' chain with many conditions

I have code that looks something like this. An 'if else' chain with each one containing a long list of conditions of when that function should get called.

if (conditionA1() || conditionA2() && conditionA3() || ... && conditionAN()) {
    functionA();
}
else if (conditionB1() || conditionB2() && conditionB3() || ... && conditionBN() {
    functionB();
}
...
else if (conditionZ1() || conditionZ2() && conditionZ3() || ... && conditionZN()) {
    functionZ();
}

It looks like messy code which could get hard to maintain and was wondering if there is a good design pattern to refactor this.

Aucun commentaire:

Enregistrer un commentaire