Intro Normal Function Before Refactor

Intro Normal Function Before Refactor

Intro Normal Function Before Refactor

Normal Function Before Refactor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);
}

上例我們會發現兩點
1.函式過長
2.區域變數幾乎在整個priavte method被使用

下一篇再來看看如何改善這個函式