CleanCode TIL (2022.02.25)

Henry Choยท2022๋…„ 2์›” 25์ผ
0

๋…ธ๊ฐœ๋ถ

๋ชฉ๋ก ๋ณด๊ธฐ
31/31

DAY 32

๐Ÿ”–ย ์˜ค๋Š˜ ์ฝ์€ ๋ฒ”์œ„ : 15. JUnit ๋“ค์—ฌ๋‹ค๋ณด๊ธฐ(334~342p)


๐Ÿค“ย ์ฑ…์—์„œ ๊ธฐ์–ตํ•˜๊ณ  ์‹ถ์€ ๋‚ด์šฉ

์‹œ๊ฐ„ ๊ฒฐํ•ฉ์„ ์™ธ๋ถ€์— ๋…ธ์ถœ

  • findCommonSuffix ์—๋Š” ์ˆจ๊ฒจ์ง„ ์‹œ๊ฐ„์ ์ธ ๊ฒฐํ•ฉ์ด ์กด์žฌ
  • findCommandSuffix๋Š” findCommonPrefix๊ฐ€ prefixIndex๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค๋Š” ์‚ฌ์‹ค์— ์˜์กดํ•˜๋ฏ€๋กœ ์ž˜๋ชป๋œ ์ˆœ์„œ๋กœ ํ˜ธ์ถœํ•˜๋ฉด ๋””๋ฒ„๊น…์ด ์–ด๋ ค์›Œ์ง
  • findCommonSuffix๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ prefixIndex๋ฅผ ์ธ์ˆ˜๋กœ ๋„˜๊ธฐ์ž
private void compactExpectedAndActual() {
    prefixIndex = findCommonPrefix();
    suffixIndex = findCommonSuffix(prefixIndex);
    compactExpected = compactString(expected);
    compactActual = compactString(actual);
}
private int findCommonSuffix(int prefixIndex) {
    int expectedSuffix = expected.length() - 1;
    int actualSuffix = actual.length() - 1;
    for (; actualSuffix >= prefixIndex && expectedSuffix >= prefixIndex; actualSuffix--, expectedSuffix--) {
        if (expected.charAt(expectedSuffix) != actual.charAt(actualSuffix))
            break;
    }
    return expected.length() - expectedSuffix;
}
  • ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์ธ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•œ ๋Š๋‚Œ์ด ์žˆ๋‹ค ๋‹ค์‹œ ์ˆ˜์ •ํ•ด๋ณด์ž
private void compactExpectedAndActual() {
    findCommonPrefixAndSuffix();
    compactExpected = compactString(expected);
    compactActual = compactString(actual);
}
private void findCommonPrefixAndSuffix() {
    findCommonPrefix();
    int expectedSuffix = expected.length() - 1;
    int actualSuffix = actual.length() - 1;
    for (; actualSuffix >= prefixIndex && expectedSuffix >= prefixIndex; actualSuffix--, expectedSuffix--) {
        if (expected.charAt(expectedSuffix) != actual.charAt(actualSuffix)) break;
    }
    suffixIndex = expected.length() - expectedSuffix;
}
private void findCommonPrefix() {
    prefixIndex = 0;
    int end = Math.min(expected.length(), actual.length());
    for (; prefixIndex < end; prefixIndex++)
        if (expected.charAt(prefixIndex) != actual.charAt(prefixIndex)) break;
}
  • findCommonPrefixAndSuffix๋กœ ๋ฌถ์–ด findCommonPrefix๋ฅผ ๋จผ์ € ํ˜ธ์ถœ
private void findCommonPrefixAndSuffix() {
    findCommonPrefix();
    int suffixLength = 1;
    for (; !suffixOverlapsPrefix(suffixLength); suffixLength++) {
        if (charFromEnd(expected, suffixLength) != charFromEnd(actual, suffixLength))
            break;
    }
    suffixIndex = suffixLength;
}
private char charFromEnd(String s, int i) {
    return s.charAt(s.length() - i);
}
private boolean suffixOverlapsPrefix(int suffixLength) {
    return actual.length() - suffixLength < prefixLength ||
        expected.length() - suffixLength < prefixLength;
}
  • findCommonPrefixAndSuffix ๋ฅผ ์ •๋ฆฌํ•˜์˜€๋”๋‹ˆ suffixIndex ๊ฐ€ ์‹ค์ œ๋กœ๋Š” 0์ด ์•„๋‹Œ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” โ€˜๊ธธ์ดโ€™ โ†’ suffixLength ๋กœ ๋ณ€๊ฒฝํ•˜์ž
public class ComparisonCompactor { ...
    private int suffixLength;
    ...
    private void findCommonPrefixAndSuffix() {
        findCommonPrefix();
        suffixLength = 0;
        for (; !suffixOverlapsPrefix(suffixLength); suffixLength++) {

            if (charFromEnd(expected, suffixLength) != charFromEnd(actual, suffixLength))
                break;
        }
    }
    private char charFromEnd(String s, int i) {
        return s.charAt(s.length() - i - 1);
    }
    private boolean suffixOverlapsPrefix(int suffixLength) {
            return actual.length() - suffixLength <= prefixLength ||
                expected.length() - suffixLength <= prefixLength;
        }
        ...
        private String compactString(String source) {
            String result =
                DELTA_START +
                source.substring(prefixLength, source.length() - suffixLength) + DELTA_END;
            if (prefixLength > 0)
                result = computeCommonPrefix() + result;
            if (suffixLength > 0)
                result = result + computeCommonSuffix();
            return result;
        }
        ...
        private String computeCommonSuffix() {
            int end = Math.min(expected.length() - suffixLength + contextLength, expected.length());
            return
            expected.substring(expected.length() - suffixLength, end) + (expected.length() - suffixLength <
                expected.length() - contextLength ?
                ELLIPSIS : "");
        }
}
  • computeCommonSuffix ์—์„œ +1์„ ์—†์•ฐ
  • charFromEnd์— -1 ์ถ”๊ฐ€
  • suffixOverlapsPrefix ์— โ‰ค ์‚ฌ์šฉ
private String compactString(String source) {
    return
    computeCommonPrefix() +
        DELTA_START +
        source.substring(prefixLength, source.length() - suffixLength) + DELTA_END +
        computeCommonSuffix();
}
  • ๋ถˆํ•„์š”ํ•œ if ๋ฌธ ์ œ๊ฑฐ if (suffixLength > 0)
    • suffixLength๊ฐ€ 1 ๊ฐ์†Œํ–ˆ์œผ๋ฏ€๋กœ โ‰ฅ ๋กœ ๊ณ ์ณ์•ผ ํ•˜์ง€๋งŒ if๋ฌธ์€ ๊ธธ์ด๊ฐ€ 0์ธ ์ ‘๋ฏธ์–ด๋ฅผ ๊ฑธ๋Ÿฌ๋‚ด ์ฒจ๋ถ€ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ if๋ฌธ์ด ์˜๋ฏธ๊ฐ€ ์—†์–ด ์ œ๊ฑฐ

๋ฒˆ๋ณต

  • ๋ฆฌํŒฉํ„ฐ๋ง ํ•˜๋‹ค๋ณด๋ฉด ์›๋ž˜ ํ–ˆ๋˜ ๋ณ€๊ฒฝ์„ ๋˜๋Œ๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ํ”ํ•˜๋‹ค. ์›๋ž˜ ์ˆ˜๋งŽ์€ ์‹œํ–‰์ฐฉ์˜ค๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ์ž‘์—…์ด๋‹ค

๊ฒฐ๋ก 

  • ์šฐ์ˆ˜ํ•œ ๋ชจ๋“ˆ์ผ์ง€๋ผ๋„ ๊ฐœ์„ ์ด ๋ถˆํ•„์š”ํ•œ ๋ชจ๋“ˆ์€ ์—†๋‹ค
  • ์ฝ”๋“œ๋ฅผ ์ฒ˜์Œ๋ณด๋‹ค ๋” ๊นจ๋—ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์ฑ…์ž„์€ ์šฐ๋ฆฌ ๋ชจ๋‘์—๊ฒŒ ์žˆ๋‹ค

๐Ÿค”ย ๋– ์˜ค๋ฅด๋Š” ์ƒ๊ฐ

๐Ÿ”Žย ์งˆ๋ฌธ

๐Ÿ“ย ์†Œ๊ฐ 3์ค„ ์š”์•ฝ

  • ๋ฆฌํŒฉํ„ฐ๋ง์€ ์›๋ž˜ ๋ฒˆ๋ณต๋„ ํ•˜๊ณ  ์ˆ˜๋งŽ์€ ์‹œํ–‰์ฐฉ์˜ค๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ์ž‘์—…์ด๋‹ค
  • ๊ฐœ์„ ์ด ๋ถˆํ•„์š”ํ•œ ๋ชจ๋“ˆ์€ ์—†๋‹ค ์šฐ์ˆ˜ํ•œ ๋ชจ๋“ˆ์ผ์ง€๋ผ๋„.
  • ์ฝ”๋“œ๋ฅผ ์ฒ˜์Œ๋ณด๋‹ค ๋” ๊นจ๋—ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ์ฑ…์ž„์€ ์šฐ๋ฆฌ ๋ชจ๋‘์—๊ฒŒ ์žˆ๋‹ค
profile
Full stack tech visionary

0๊ฐœ์˜ ๋Œ“๊ธ€