Replacing nested if statements

前端 未结 11 1904
春和景丽
春和景丽 2020-12-23 20:44

This is related to a chapter from beautiful code. And in that chapter I read about the nested ifs.

The author was talking about deeply nested if

相关标签:
11条回答
  • 2020-12-23 21:27

    Decision tables are where you store the conditional logic in a data structure rather than within the code itself.

    So instead of this (using @Pax's example):

    if (i == 1) {
        // action 1
    } else {
        if (i == 2) {
            // action 2
        } else {
            if (i == 3) {
                // action 3
            } else {
                // action 4
            }
        }
    }
    

    you do something like this:

    void action1()
    {
        // action 1
    }
    
    void action2()
    {
        // action 2
    }
    
    void action3()
    {
        // action 3
    }
    
    void action4()
    {
        // action 4
    }
    
    #define NUM_ACTIONS 4
    
    // Create array of function pointers for each allowed value of i
    void (*actions[NUM_ACTIONS])() = { NULL, action1, action2, action3 }
    
    // And now in the body of a function somewhere...
    if ((i < NUM_ACTIONS) && actions[i])
        actions[i]();
    else
        action4();
    

    If the possibilities for i are not low-numbered integers then you could create a lookup table instead of directly accessing the ith element of the actions array.

    This technique becomes much more useful than nested ifs or switch statements when you have a decision over dozens of possible values.

    0 讨论(0)
  • 2020-12-23 21:30

    If and switch statements are not purely OO. They are conditional procedural logic, but do a very good job! If you want to remove these statements for a more OO approach, combine the 'State' and 'Descriptor' patterns.

    0 讨论(0)
  • 2020-12-23 21:32

    Well, not directly an answer to your question since you specifically ask about switch/case statements, but here is a similar question.

    Invert “if” statement to reduce nesting

    This talks about replacing nested if's with guard-statements, that return early, instead of progressively checking more and more things before settling on a return value.

    0 讨论(0)
  • 2020-12-23 21:32

    How about chained ifs?

    Replace

    if (condition1)
    {
        do1
    }   
    else
    {
        if (condition2)
        {
            do2
        }
        else (condition3)
        {
            do3;
    
        }
    }
    

    with

    if (condition1) {
       do1;
    } else if (condition2) {
       do2;
    } else if (condition3) {
       do3;
    }
    

    This is much like switch statement for complex conditions.

    0 讨论(0)
  • 2020-12-23 21:32

    You might also consider using the Visitor pattern.

    0 讨论(0)
  • 2020-12-23 21:35

    One example I always try to do is replace heavily nested if's like this (actually this one's not too bad but I've seen them up to 8 or 9 levels deep in the wild):

    if (i == 1) {
        // action 1
    } else {
        if (i == 2) {
            // action 2
        } else {
            if (i == 3) {
                // action 3
            } else {
                // action 4
            }
        }
    }
    

    with this:

    switch (i) {
        case 1:
            // action 1
            break;
        case 2:
            // action 2
            break;
        case 3:
            // action 3
            break;
        default:
            // action 4
            break;
    }
    

    I also try to keep the actions as small as possible (function calls are best for this) to keep the switch statement compressed (so you don't have to go four pages ahead to see the end of it).

    Decision tables, I believe, are simply setting flags indicating what actions have to be taken later on. The "later on" section is simple sequencing of actions based on those flags. I could be wrong (it won't be the first or last time :-).

    An example would be (the flag-setting phase can be complicated if's since its actions are very simple):

    switch (i) {
        case 1:
            outmsg = "no paper";
            genmsg = true;
            mailmsg = true;
            phonemsg = false;
            break;
        case 2:
            outmsg = "no ink";
            genmsg = true;
            mailmsg = true;
            phonemsg = false;
            break;
        default:
            outmsg = "unknown problem";
            genmsg = true;
            mailmsg = true;
            phonemsg = true;
            break;
    }
    
    if (genmsg)
        // Send message to screen.
    if (mailmsg)
        // Send message to operators email address.
    if (phonemsg)
        // Hassle operators mobile phone.
    
    0 讨论(0)
提交回复
热议问题