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
The Smooth
method can be simplified (or in your term: optimized?) in some ways:
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.if (a || b)
case, the left expression (that is: a
) will be evaluated first - this is known as Short Circuit Evaluation.if-else
block.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();
}
}