【发布时间】:2016-03-26 20:01:11
【问题描述】:
我有一个 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;
}
【问题讨论】:
-
您尝试分析了吗?顺便说一句,我将只定义一次
Random并将这个大方法分成几个较小的方法 (Ctrl+R, M) -
我知道这不是你问的,但这是对
new Random的非常危险的用法;如果您在循环中调用Smooth,它可以使用相同的种子。您应该将rand设为实例变量并在构造函数中对其进行初始化。 -
感谢您的回答!不,我没有尝试分析,我会修复
new Random -
定义优化。您是否想分析输入值并尽可能快地重构最常见输入的代码,而不管它有多神秘?您希望以尽可能少的字符表示代码吗?最少的内存缓存未命中? ...
-
问“你能把这段代码写多短?”的地方是在拼图网站上问一个代码高尔夫问题。但较短的代码很少是更易于理解、更快、更好的代码。它只是更短。如果你想要的是让你的代码更优雅和更容易理解,那么这与让它更短有很大的不同。首先消除所有这些参考。如果您正在计算值,请将值传回而不是修改变量。
标签: c# .net optimization readability