public void ScoreFirstBall(int pinsKnockedDown) {
if (IsStrike(Frame, pinsKnockedDown)) {
Score = "X";
ScoreMessage = "Good Job";
} else if (IsGutterBall(pinsKnockedDown)) {
Score = "0";
ScoreMessage = "You'll do better next time";
}
PinsTotal += pinsKnockedDown;
}
public void ScoreSecondBall(int pinsKnockedDown) {
PinsTotal += pinsKnockedDown;
}
Questions:
-
In this case I want
PinsTotal += pinsKnockedDownto be called every timeScoreSecondBall(...)orScoreFirstBall(...)runs. So I want to put it in a function (it may expand to many lines in the future). -
Is it efficient to do this? Because we have a thread to listen when functions are called.
Thanks for reading my question, if it’s not clear, just comment and I will fix it 🙂
You could refactor that code to use only one
ScoreBall()function that takes an additional argument:Then either call it directly or, if you want to keep your existing functions, have them delegate to the common implementation:
This way, you don’t have to worry about putting the incrementation of
PinsTotalinto its own function.As for efficiency, function calls do have a cost, but it’s usually negligible compared to the other bottlenecks that might exist in the code.