JUnit 들여다보기(2)

책너두 5기 36일차 로버트 C. 마틴의 클린코드 p. 333~ p.342 내용정리 15. JUnit 들여다보기 리팩터링 하기 명명법 제대로 해서 이해하기 쉽게 하기 숨겨진 시간적인 결합(hidden temporal coupling) 확인하기 고치면서 조건문의 조건을 변겨앻야 하는지 확인하기 연산자에 등호 포함 여부 확인하기 세상에 개선이 불필요한 모듈은 없다. 코드를 처음보다 조금 더 깨끗하게 만드는 책임은 우리 모두에게 있다.

2023년 9월 16일 · 1 분 · 배준수

JUnit 들여다보기(1)

책너두 5기 35일차 로버트 C. 마틴의 클린코드 p. 324~ p.332 34일차는 없습니다. 내용정리 15. JUnit 들여다보기 JUnit : 가장 유명한 자바 프레임워크 JUnit 프레임워크 과거의 모듈을 리팩토링해보자. ComparisonCompactor 모듈은 문자열 비교 오류를 파악할 때 쓴다. 원본은 다음과 같다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 package junit.framework; public class ComparisonCompactor { private static final String ELLIPSIS = "..."; private static final String DELTA_END = "]"; private static final String DELTA_START = "["; private int fContextLength; private String fExpected; private String fActual; private int fPrefix; private int fSuffix; public ComparisonCompactor(int contextLength, String expected, String actual) { fContextLength = contextLength; fExpected = expected; fActual = actual; } public String compact(String message) { if (fExpected == null || fActual == null || areStringsEqual()) { return Assert.format(message, fExpected, fActual); } findCommonPrefix(); findCommonSuffix(); String expected = compactString(fExpected); String actual = compactString(fActual); return Assert.format(message, expected, actual); } private String compactString(String source) { String result = DELTA_START + source.substring(fPrefix, source.length() - fSuffix + 1) + DELTA_END; if (fPrefix > 0) { result = computeCommonPrefix() + result; } if (fSuffix > 0) { result = result + computeCommonSuffix(); } return result; } private void findCommonPrefix() { fPrefix = 0; int end = Math.min(fExpected.length(), fActual.length()); for (; fPrefix < end; fPrefix++) { if (fExpected.charAt(fPrefix) != fActual.charAt(fPrefix)) { break; } } } private void findCommonSuffix() { int expectedSuffix = fExpected.length() - 1; int actualSuffix = fActual.length() - 1; for (; actualSuffix >= fPrefix && expectedSuffix >= fPrefix; actualSuffix--, expectedSuffix--) { if (fExpected.charAt(expectedSuffix) != fActual.charAt(actualSuffix)) { break; } } fSuffix = fExpected.length() - expectedSuffix; } private String computeCommonPrefix() { return (fPrefix > fContextLength ? ELLIPSIS : "") + fExpected.substring(Math.max(0, fPrefix - fContextLength), fPrefix); } private String computeCommonSuffix() { int end = Math.min(fExpected.length() - fSuffix + 1 + fContextLength, fExpected.length()); return fExpected.substring(fExpected.length() - fSuffix + 1, end) + (fExpected.length() - fSuffix + 1 < fExpected.length() - fContextLength ? ELLIPSIS : ""); } private boolean areStringsEqual() { return fExpected.equals(fActual); } } 가장 먼저 멤버 변수 앞에 붙인 접두어 f를 삭제하자. 과거의 습관이다. 이후 compact 함수 시작부에 캡슐화되지 않은 조건문을 메서드로 뽑아내 적절한 이름을 붙인다. f를 빼면서 생긴 비슷한 이름들을 다시 명명한다. if 부정문을 긍정문으로 바꾸어 가독성을 높인다. ...

2023년 9월 15일 · 3 분 · 배준수

의미있는 이름(2)

책너두 5기 5일차 로버트 C. 마틴의 클린코드 p.35 ~ p.45 내용 정리 2장 의미있는 이름 의미 있는 맥락을 추가하라 state만 보고는 주소인지, 상태인지 알 수없다. addrstate면 주소에서 ‘주’를 뜻한다는 것을 파악할 수 있다. 예시) 맥락이 불분명한 함수 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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"; pluralModifer = "s"; } String guessMessage = String.format( "There %s %s %s%s", verb, number, candidate, pluralModifier ); print(guessMessage); } GuessStatisticsMessage 클래스를 만든 후 세 변수를 넣는다. ...

2023년 8월 4일 · 5 분 · 배준수