Replies: 2 comments 6 replies
-
좀 더 이른 시점에 질문을 드렸어야 했는데 쪼꼼 늦었습니다. https://d2.naver.com/helloworld/1250 의 글을 참조하다 보니 꽤 오래된 글임에도 불구하고 방어적 코드에 대한 작성이 있었더라고요. protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
Matcher patternMatcher = regex.matcher(value.toString());
// 아파치 서버 웹 로그 패턴이 정의한 로그 패턴과 일치하는지 체크
if (patternMatcher.matches()) {
accessUrl = StringUtils.trimToEmpty(patternMatcher.group(2)); // client가 실제로 request 요청한 URL 부분
responseCode = StringUtils.trimToEmpty(patternMatcher.group(3)); // request에 대한 responset code 부분
// 요청 URL이 음식 End Page이고, HTTP 응답코드가 200(OK)일 경우
if (accessUrl.indexOf(FOOD_URL_PATTERN) > -1 && responseCode.startsWith("200")) {
codeValue = StringUtils.substringBetween(accessUrl, CODE_BEFORE_TEXT, CODE_AFTER_TEXT);
// 코드값(codeValue)이 없을 경우, codeValue는 null이 할당되므로 아래 trimToEmpty로 방어코드.
foodId.set(StringUtils.trimToEmpty(codeValue));
context.write(foodId, one);
}
}
}
라는 부분이 보였고 해당 StringUtils의 trimToEmpty 메서드는 문자열을 인자로 받아서, 해당 문자열이 null이거나 공백 문자열인 경우 빈 문자열("")을 반환하는 메서드입니다. 어떤 이유 때문에 해당 코드가 방어적 코드로서의 동작을 하는것일까요? |
Beta Was this translation helpful? Give feedback.
-
내용 잘 읽었습니다~ 하나 궁금한 게, 이전에 시니어 개발자 중 한 분에게 가변 객체와 불변 객체가 있을 때 무조건 불변 객체가 좋나요? 라고 질문한 적이 있습니다. 저도 무지성 setter 사용을 지양하고 불변 객체를 선호하지만, 한편으로는 객체를 생성하는 비용을 생각하지 않을 수 없습니다. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
방어적 복사본 적용 예 - 1
방어적 복사본 적용 예 - 2
결론
Beta Was this translation helpful? Give feedback.
All reactions