clean code ์ 15์ฅ JUnit ๋ค์ฌ๋ค๋ณด๊ธฐ
Java Framework JUnit์ ํ ์คํธ ์ฝ๋ ๊ตฌ์กฐ ๋ถ์์ ํตํ์ฌ
ํ ์คํธ ์ฝ๋๋ฅผ ์ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์๊ฐ๋ ์ฅ์ ๋๋ค.
1. ์ ๋์ด f๋ฅผ ์ ๊ฑฐํ๋ฉด ๊น๋ํจ
1.
private int fContextLength;
private String fExpected;
private String fActual;
private int fPrefix;
private int fSuffix;
2.
private int contextLength;
private String expected;
private String actual;
private int prefix;
private int suffix;
2. ์กฐ๊ฑด๋ฌธ์ ์บก์ํํ๋ฉด ์ข์
1.
public String compact(String message) {
if (expected == null || actual == null || areStringsEqual()) {
return Assert.format(message, expected, actual);
}
findCommonPrefix();
findCommonSuffix();
String expected = compactString(this.expected);
String actual = compactString(this.actual);
return Assert.format(message, expected, actual);
}
2.
public String compact(String message) {
if (shouldNotCompact()) {
return Assert.format(message, expected, actual);
}
findCommonPrefix();
findCommonSuffix();
String expected = compactString(this.expected);
String actual = compactString(this.actual);
return Assert.format(message, expected, actual);
}
private boolean shouldNotCompact() {
return expected == null || actual == null || areStringsEqual();
}
3. ๋ณ์๋ช ์ ๋ช ํํ ํ๊ธฐ
1.
String expected = compactString(this.expected);
String actual = compactString(this.actual);
2.
String compactExpected = compactString(expected);
String compactActual = compactString(actual);
4. ๋ถ์ ๋ฌธ๋ณด๋ค๋ ๊ธ์ ๋ฌธ์ผ๋ก
1.
public String compact(String message) {
if (shouldNotCompact()) {
return Assert.format(message, expected, actual);
}
findCommonPrefix();
findCommonSuffix();
String expected = compactString(this.expected);
String actual = compactString(this.actual);
return Assert.format(message, expected, actual);
}
private boolean shouldNotCompact() {
return expected == null || actual == null || areStringsEqual();
}
2.
public String compact(String message) {
if (canBeCompacted()) {
findCommonPrefix();
findCommonSuffix();
String compactExpected = compactString(expected);
String compactActual = compactString(actual);
return Assert.format(message, compactExpected, compactActual);
} else {
return Assert.format(message, expected, actual);
}
}
private boolean canBeCompacted() {
return expected != null && actual != null && !areStringsEqual();
}
5. ํจ์๋ช ์ ์์๋ณผ ์ ์๊ฒ ํ์ด์ ์์ฑํ๊ธฐ
1.
public String compact(String message) { }
2.
public String formatCompactedComparison(String message) { }
๋ฌธ์์ด์ ์์ถํ๋ ๋ฉ์๋ compact() ์ด์ง๋ง, ํ์์ ๋ง์ถ์ด ๋ฐํ๊น์ง ํ๊ณ ์์.
6. ํจ์๋ ํ๊ฐ์ง ์ผ๋ง ํ๋๋ก ์์ฑ
1.
public String compact(String message) {
if (canBeCompacted()) {
findCommonPrefix();
findCommonSuffix();
String compactExpected = compactString(expected);
String compactActual = compactString(actual);
return Assert.format(message, compactExpected, compactActual);
} else {
return Assert.format(message, expected, actual);
}
}
private boolean canBeCompacted() {
return expected != null && actual != null && !areStringsEqual();
}
2.
private String compactExpected;
private String compactActual;
public String formatCompactedComparison(String message) {
if (canBeCompacted()) {
compactExpectedAndActual();
return Assert.format(message, compactExpected, compactActual);
} else {
return Assert.format(message, expected, actual);
}
}
private void compactExpectedAndActual() {
findCommonPrefix();
findCommonSuffix();
compactExpected = compactString(expected);
compactActual = compactString(actual);
}
๋ฌธ์์ด์ ์์ถํ๋ ๋ก์ง๊ณผ
foramt์ ๋ง์ถ๋ ๋ก์ง์ ๋ถ๋ฆฌ
๊ฐ ํจ์๊ฐ ํ๋์ ๊ธฐ๋ฅ์ ํ๋๋ก ํ๋ฉด ์ข์
7. ํจ์ ๋ด๋ถ ์ถ์ํ ์์ค์ ๋์ผํ ๋ ๋ฒจ๋ก ์ผ๊ด์ ์ด๊ฒ ๊ตฌํํ๋ฉด ์ข์
1.
private void compactExpectedAndActual() {
findCommonPrefix();
findCommonSuffix();
compactExpected = compactString(expected);
compactActual = compactString(actual);
}
2.
private compactExpectedAndActual() {
prefixIndex = findCommonPrefix();
suffixIndex = findCommonSuffix();
String compactExpected = compactString(expected);
String compactActual = compactString(actual);
}
private int findCommonPrefix() {
int prefixIndex = 0;
int end = Math.min(expected.length(), actual.length());
for (; prefixIndex < end; prefixIndex++) {
if (expected.charAt(prefixIndex) != actual.charAt(prefixIndex)) {
break;
}
}
return prefixIndex;
}
private int findCommonSuffix() {
int expectedSuffix = expected.length() - 1;
int actualSuffix = actual.length() - 1;
for (; actualSuffix >= prefixIndex && expectedSuffix >= prefix; actualSuffix--, expectedSuffix--) {
if (expected.charAt(expectedSuffix) != actual.charAt(actualSuffix)) {
break;
}
}
return expected.length() - expectedSuffix;
}
ํ๋์ ํจ์์ ๋ฐํ๊ฐ์ด ์กด์ฌํ๋ ํจ์์ ๋ฐํ๊ฐ์ด ์กด์ฌํ์ง ์๋ ํจ์๋ฅผ ํผํฉํ์ฌ ์ฌ์ฉ์ค์ธ๋ฐ,
ํต์ผํ๋ฉด ์ข์.
8. ์จ๊ฒจ์ง ์๊ฐ์ ์ธ ๊ฒฐํฉ์ฃผ์
1.
private compactExpectedAndActual() {
prefixIndex = findCommonPrefix();
suffixIndex = findCommonSuffix();
String compactExpected = compactString(expected);
String compactActual = compactString(actual);
}
private int findCommonPrefix() {
int prefixIndex = 0;
int end = Math.min(expected.length(), actual.length());
for (; prefixIndex < end; prefixIndex++) {
if (expected.charAt(prefixIndex) != actual.charAt(prefixIndex)) {
break;
}
}
return prefixIndex;
}
private int findCommonSuffix() {
int expectedSuffix = expected.length() - 1;
int actualSuffix = actual.length() - 1;
for (; actualSuffix >= prefixIndex && expectedSuffix >= prefix; actualSuffix--, expectedSuffix--) {
if (expected.charAt(expectedSuffix) != actual.charAt(actualSuffix)) {
break;
}
}
return expected.length() - expectedSuffix;
}
2.
private compactExpectedAndActual() {
prefixIndex = findCommonPrefix();
suffixIndex = findCommonSuffix(prefixIndex);
String compactExpected = compactString(expected);
String compactActual = compactString(actual);
}
private int findCommonSuffix(int prefixIndex) {
int expectedSuffix = expected.length() - 1;
int actualSuffix = actual.length() - 1;
for (; actualSuffix >= prefixIndex && expectedSuffix >= prefix; actualSuffix--, expectedSuffix--) {
if (expected.charAt(expectedSuffix) != actual.charAt(actualSuffix)) {
break;
}
}
return expected.length() - expectedSuffix;
}
๋๋ฒ์งธ ํจ์์ธ findCommonSuffix ํจ์์์ ์ฐธ์กฐํ๋ ์ ์ญ๋ณ์ prefixIndex๋ ์ฒซ๋ฒ์งธ findCommonPrefix์ ๋ฆฌํด๊ฐ์ ๋ฐ๋๋ค. ์ด๋ ๊ฒ ์ฒซ๋ฒ์งธ ํจ์ ๋ฆฌํด๊ฐ์ ์์กดํ๊ณ ์๋๋ฐ ์์๊ฐ ๋ฌ๋ผ์ง๋ฉด ๊ฒฐ๊ณผ๊ฐ์ด ๋ฌ๋ผ์ง๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
์ ์ญ๋ณ์ ๋์ ๋งค๊ฐ๋ณ์๋ก ์ ๋ ฅ๋ฐ์ ์ฌ์ฉํ๋ฉด ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ง ์์.
9. Length์ Index๋ฅผ ๊ตฌ๋ถํด์ ์ฌ์ฉํ๊ธฐ
๋ณ์์ด๋ฆ์ Index๋ฅผ ๋ฃ์๋๋
0๋ถํฐ ์์ํ๋ ๊ฐ์ด์ฌ์ผํ๊ณ
1๋ถํฐ ์์ํ ๊ฒฝ์ฐ Index ๋์ Length ๋ผ๋ ์ด๋ฆ์ ์ฌ์ฉํ๋ฉด ์ข์.