Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法优化_C#_.net_Optimization_Readability - Fatal编程技术网

C# 方法优化

C# 方法优化,c#,.net,optimization,readability,C#,.net,Optimization,Readability,我有一个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

我有一个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);
                    }
                }
            }
        }
    }
折叠
方法

    private static double RoundN(int sChips, int n) {
        double a = Math.Round((sChips / n) / 100d, 0) * 100;
        return a;
    }
    private void Fold(ref bool sTurn, ref bool SFTurn, Label sStatus) {
        raising = false;
        sStatus.Text = "Fold";
        sTurn = false;
        SFTurn = true;
    }
    private void Check(ref bool cTurn, Label cStatus) {
        cStatus.Text = "Check";
        cTurn = false;
        raising = false;
    }
    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();
    }
    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;
    }
检查
方法

    private static double RoundN(int sChips, int n) {
        double a = Math.Round((sChips / n) / 100d, 0) * 100;
        return a;
    }
    private void Fold(ref bool sTurn, ref bool SFTurn, Label sStatus) {
        raising = false;
        sStatus.Text = "Fold";
        sTurn = false;
        SFTurn = true;
    }
    private void Check(ref bool cTurn, Label cStatus) {
        cStatus.Text = "Check";
        cTurn = false;
        raising = false;
    }
    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();
    }
    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;
    }
调用
方法

    private static double RoundN(int sChips, int n) {
        double a = Math.Round((sChips / n) / 100d, 0) * 100;
        return a;
    }
    private void Fold(ref bool sTurn, ref bool SFTurn, Label sStatus) {
        raising = false;
        sStatus.Text = "Fold";
        sTurn = false;
        SFTurn = true;
    }
    private void Check(ref bool cTurn, Label cStatus) {
        cStatus.Text = "Check";
        cTurn = false;
        raising = false;
    }
    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();
    }
    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;
    }
提出的
方法

    private static double RoundN(int sChips, int n) {
        double a = Math.Round((sChips / n) / 100d, 0) * 100;
        return a;
    }
    private void Fold(ref bool sTurn, ref bool SFTurn, Label sStatus) {
        raising = false;
        sStatus.Text = "Fold";
        sTurn = false;
        SFTurn = true;
    }
    private void Check(ref bool cTurn, Label cStatus) {
        cStatus.Text = "Check";
        cTurn = false;
        raising = false;
    }
    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();
    }
    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
方法可以通过某些方式简化(或者用您的术语:优化?):

  • 移除条件(<代码> If Enter/代码>)嵌套块,考虑使用早期<代码>返回<代码>对于两个之间比较简单或没有进一步延续的条件。这样,就可以删除“难以读取”的嵌套块
  • 为避免“重复”块,具有相同动作的块应视为分组在一起,而不是分开
  • 想一想,反转条件是否有助于简化代码
  • 利用你所知道的关于语言评估的任何有益行为。例如,在像
    if(a | | b)
    这样的条件语句的参数中,将首先计算表达式(即:
    a
    ),这称为
  • 只要有可能,并且没有明显失去可读性,考虑使用替换<代码>如果是< /Cord>块。
  • 声明您将多次使用的变量,而不会只更改一次值
  • 注意重叠(加倍/重复)情况
  • 使用正确的数据类型将有帮助 对于您的情况,简化代码可以是这样的

    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();
        }
    }
    
    uint轮数=0//读8。
    无效平滑(参考整数botChips、参考布尔botTurn、标签botStatus、整数名称、整数n、整数r){
    Random rand=新的Random();
    int rnd=rand.Next(1,3);
    
    如果(你试过评测吗?顺便说一句,我只会定义一次
    Random
    ,然后将这个大方法分成几个小方法(
    Ctrl+R,M
    )我知道这不是你问的,但这是一个非常危险的使用
    new Random
    ;如果你在循环中调用
    Smooth
    ,它可能使用相同的种子。你应该使
    rand
    成为一个实例变量,并在构造函数中初始化它。谢谢你的回答!不,我没有尝试分析,我会修复
    new Random
    Define optimize。是否要分析输入值并重新构造代码,使其尽可能快地用于最常见的输入,而不管其含义有多神秘?是否希望代码以尽可能少的字符表示?内存缓存未命中率最低?…询问“您可以将此代码缩短多少?”就是在益智网站上问一个代码高尔夫问题。但是较短的代码很少更容易理解、更快、更好的代码。它只是较短。如果你想让代码更优雅、更易懂,那么这与缩短代码非常不同。从消除所有这些引用开始。如果你在计算值,请将值传递回原处而不是修改变量。