computer science

clean code ์ œ 15์žฅ JUnit ๋“ค์—ฌ๋‹ค๋ณด๊ธฐ

ggasoon2 2023. 6. 4. 22:30

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 ๋ผ๋Š” ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์Œ.