lundi 3 avril 2017

Duplicate logic in Nested If Statements

I've run into this situation a few times now, where I'll have a logic structure that looks something like this.

switch(someInteger) 
{
    case 1:
        if(conditionA) {
            if(conditionB) {
                 func1();
            } else {
                 func2();
            }
        } else {
            func3();
        }
        break;

    case 2:
        if(conditionA) {
            if(conditionB) {
                 func4();
            } else {
                 func5();
            }
        } else {
            func6();
        }
        break;
}

The structure in each case is identical, the only differences are the functions being called. But now I'm duplicating my logic, and that just feels dirty. I feel like there's a more elegant way to handle these situations, but I've yet to come across one.

Any ideas on how to refactor this?

Aucun commentaire:

Enregistrer un commentaire