方法优化

我有一个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 = 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 = 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) case的条件语句的参数中,将首先计算表达式(即: a ) – 这称为短路评估 。
  5. 只要有可能,并且没有明显失去可读性,请考虑使用Ternary运算符替换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(); } }