Skip to content

Instantly share code, notes, and snippets.

@luisenriquecorona
Created February 26, 2020 01:08
Show Gist options
  • Save luisenriquecorona/1e3e285eaa551a74c5cbad5deb7c077f to your computer and use it in GitHub Desktop.
Save luisenriquecorona/1e3e285eaa551a74c5cbad5deb7c077f to your computer and use it in GitHub Desktop.
The function is a bit too long and the variables are used throughout. To split the func- tion into smaller pieces we need to create a GuessStatisticsMessage class and make the three variables fields of this class. This provides a clear context for the three variables. They are definitively part of the GuessStatisticsMessage.
private void printGuessStatistics(char candidate, int count) {
String number;
String verb;
String pluralModifier;
if (count == 0) {
number = "no";
verb = "are";
pluralModifier = "s";
} else if (count == 1) {
number = "1";
verb = "is";
pluralModifier = "";
} else {
number = Integer.toString(count);
verb = "are";
pluralModifier = "s";
}
String guessMessage = String.format(
"There %s %s %s%s", verb, number, candidate, pluralModifier
);
print(guessMessage);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment