Method optimization

前端 未结 1 1839
我在风中等你
我在风中等你 2021-01-16 09:04

I have a void function that has a lot of if statements in it and all of them are required I really can\'t remove anything. But I feel like that it could be done better. Usin

相关标签:
1条回答
  • 2021-01-16 09:40

    The Smooth method can be simplified (or in your term: optimized?) in some ways:

    1. To remove the conditional (if-else) nested blocks, consider the use of early return for conditions which are simpler among the two or has no further continuation. This way, you may remove "difficult-to-read" nested blocks.
    2. To avoid "duplicate" blocks, blocks with identical actions should be considered to be grouped together rather than being separated.
    3. Think if reversing the condition can help to simplify your code
    4. Exploit whatever beneficial behaviors that you know about the language evaluation. For instance, for C#, in the argument of the conditional statement like if (a || b) case, the left expression (that is: a) will be evaluated first - this is known as Short Circuit Evaluation.
    5. Whenever possible, and without significantly losing the readability, consider of using Ternary operator to replace if-else block.
    6. Declare variable that you will be using multiple times without changing the value only once
    7. Watch out for overlapping (doubled/duplicated) conditions!
    8. Use correct data type will help!

    For your case, the simplified code can be something like this

    uint rounds = 0; //read 8.
    void Smooth(ref int botChips, ref bool botTurn, Label botStatus, int name, int n, int r) {
        Random rand = new Random();
        int rnd = rand.Next(1, 3);
        if (rounds <= 1) { //read 8.
            if (call <= 0) {
                Check(ref botTurn, botStatus); //since your Check doesn't change rounds, this is legal                  
                return; //read 1. early return                  
            } //beyond this call > 0
            if (call >= RoundN(botChips, n) || botChips < call * 2) { //read 2., 3., 4., and 7.
                Call(ref botChips, ref botTurn, botStatus);
                return; //read 1.
            } //beyond this is the opposite of both conditions
            Raise *= 2;
            Raised(ref botChips, ref botTurn, botStatus);
        }
    
        if (rounds == 2 || rounds == 3) {
            if (call <= 0) {
                if (rnd == 1) { //call <= 0, rnd == 1, similar to the block on call < rNBChips, may potentially be further simplified
                    Raise = RoundN(botChips, r);
                    Raised(ref botChips, ref botTurn, botStatus);
                } else if (rounds == 2) //read 7. rnd is definitely not 1, no need for further check
                    Check(ref botTurn, botStatus);
                return; //read 1. this is valid since you don't want to continue
            }
            double rNBChips = RoundN(botChips, r); //read 6. this way you avoid multiple calls. It both shorter and faster
            if (call < rNBChips) { //read 3.
                Raise = Raise <= rNBChips / 2 ? rNBChips : Raise * 2; //read 5.
                Raised(ref botChips, ref botTurn, botStatus);
                return; // read 1.
            }
            if (botChips > call) {
                Call(ref botChips, ref botTurn, botStatus);
                return; //read 1.
            }
            raising = false;
            botTurn = false;
            botChips = 0;
            botStatus.Text = "Call " + call;
            tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
        }
    }
    

    Without the comments it even looks a lot more compact, like this

    uint rounds = 0;
    void Smooth(ref int botChips, ref bool botTurn, Label botStatus, int name, int n, int r) {
        Random rand = new Random();
        int rnd = rand.Next(1, 3);
        if (rounds <= 1) {
            if (call <= 0) {
                Check(ref botTurn, botStatus);              
                return; 
            }
            if (call >= RoundN(botChips, n) || botChips < call * 2) {
                Call(ref botChips, ref botTurn, botStatus);
                return;
            }
            Raise *= 2;
            Raised(ref botChips, ref botTurn, botStatus);
        }
    
        if (rounds == 2 || rounds == 3) {
            if (call <= 0) {
                if (rnd == 1) {
                    Raise = RoundN(botChips, r);
                    Raised(ref botChips, ref botTurn, botStatus);
                } else if (rounds == 2)
                    Check(ref botTurn, botStatus);
                return;
            }
            double rNBChips = RoundN(botChips, r);
            if (call < rNBChips) {
                Raise = Raise <= rNBChips / 2 ? rNBChips : Raise * 2;
                Raised(ref botChips, ref botTurn, botStatus);
                return;
            }
            if (botChips > call) {
                Call(ref botChips, ref botTurn, botStatus);
                return;
            }
            raising = false;
            botTurn = false;
            botChips = 0;
            botStatus.Text = "Call " + call;
            tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
        }
    }
    
    0 讨论(0)
提交回复
热议问题