方法优化

本文关键字:优化 方法 | 更新日期: 2023-09-27 18:27:25

我有一个void函数,里面有很多if语句,所有这些语句都是必需的,我真的无法删除任何内容。但我觉得这本可以做得更好。使用一些LINQ.Where、类或类似的东西。我想用尽可能少的字符优化和表达void Smooth

    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 == 0 || rounds == 1)
        {
            if (call <= 0)
            {
                Check(ref botTurn, botStatus);
            }
            else
            {
                if (call >= RoundN(botChips, n))
                {
                    Call(ref botChips, ref botTurn, botStatus);
                }
                else
                {
                    if (botChips >= call * 2)
                    {
                        Raise *= 2;
                        Raised(ref botChips, ref botTurn, botStatus);
                    }
                    else
                    {
                        Call(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 (rnd!=1 && rounds==2)
                {
                    Check(ref botTurn, botStatus);
                }
            }
            else
            {
                if (call >= RoundN(botChips, r))
                {
                    if (botChips > call)
                    {
                        Call(ref botChips, ref botTurn, botStatus);
                    }
                    if (botChips <= call)
                    {
                        raising = false;
                        botTurn = false;
                        botChips = 0;
                        botStatus.Text = "Call " + call;
                        tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
                    }
                }
                else
                {
                    if (Raise <= (RoundN(botChips, r)) / 2)
                    {
                        Raise = RoundN(botChips, r);
                        Raised(ref botChips, ref botTurn, botStatus);
                    }
                    else
                    {
                        Raise *= 2;
                        Raised(ref botChips, ref botTurn, botStatus);
                    }
                }
            }
        }
    }

RoundN方法

    private static double RoundN(int sChips, int n) {
        double a = Math.Round((sChips / n) / 100d, 0) * 100;
        return a;
    }

Fold方法

    private void Fold(ref bool sTurn, ref bool SFTurn, Label sStatus) {
        raising = false;
        sStatus.Text = "Fold";
        sTurn = false;
        SFTurn = true;
    }

Check方法

    private void Check(ref bool cTurn, Label cStatus) {
        cStatus.Text = "Check";
        cTurn = false;
        raising = false;
    }

Call方法

    private void Call(ref int sChips, ref bool sTurn, Label sStatus) {
        raising = false;
        sTurn = false;
        sChips -= call;
        sStatus.Text = "Call " + call;
        tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
    }

Raised方法

    private void Raised(ref int sChips, ref bool sTurn, Label sStatus) {
        sChips -= Convert.ToInt32(Raise);
        sStatus.Text = "Raise " + Raise;
        tbPot.Text = (int.Parse(tbPot.Text) + Convert.ToInt32(Raise)).ToString();
        call = Convert.ToInt32(Raise);
        raising = true;
        sTurn = false;
    }

方法优化

Smooth方法可以在某些方面进行简化(或者用你的话说:优化?):

  1. 要删除条件(if-else)嵌套块,请考虑将早期return用于两个条件中较简单或没有进一步延续的条件。通过这种方式,您可以删除"难以读取"的嵌套块
  2. 为了避免"重复"块,具有相同操作的块应被视为分组在一起,而不是分开
  3. 想一想反转条件是否有助于简化代码
  4. 利用你所知道的关于语言评估的任何有益行为。例如,对于C#,在条件语句(如if (a || b))的自变量中,将首先评估表达式(即:a),这被称为短路评估
  5. 只要可能,在不明显失去可读性的情况下,考虑使用三元运算符来替换if-else
  6. 声明将多次使用的变量,而不更改值一次
  7. 注意重叠(加倍/重复)情况
  8. 使用正确的数据类型将有帮助

对于您的情况,简化的代码可以类似于以下

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();
    }
}

没有评论,它甚至看起来更紧凑,就像这个

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();
    }
}