Matcher.java
더보기
public class Matcher {
public Matcher() {}
public boolean match(int[] expected, int[] actual,
int clipLimit, int delta)
{
// Clip "too-large" values
for (int i = 0; i < actual.length; i++)
if (actual[i] > clipLimit)
actual[i] = clipLimit;
// Check for length differences
if (actual.length != expected.length)
return false;
// Check that each entry within expected +/- delta
for (int i = 0; i < actual.length; i++)
if (Math.abs(expected[i] - actual[i]) > delta)
return false;
return true;
}
}
A. Extract Method를 이용해 match()메서드 안에 주석이 필요 없도록 고쳐 보자.
주석을 제거해도 코드의 가독성을 높히고, 재사용성등을 높이기 위해
Extract Method를 수행한다.
더보기
public class Matcher {
public Matcher() {}
public boolean match(int[] expected, int[] actual,
int clipLimit, int delta)
{
clipTooLarge(actual, clipLimit);
if (checkEachLength(expected, actual)) return false;
if (checkEach(delta, expected, actual)) {
return false;
}
return true;
}
private void clipTooLarge(int[] actual, int clipLimit) {
for (int i = 0; i < actual.length; i++)
if (actual[i] > clipLimit)
actual[i] = clipLimit;
}
private boolean checkEachLength(int[] expected, int[] actual) {
if (actual.length != expected.length) {
return true;
}
return false;
}
private boolean checkEach(int delta, int[] expected, int[] actual) {
for (int i = 0; i < actual.length; i++) {
if (Math.abs(expected[i] - actual[i]) > delta) {
return true;
}
}
return false;
}
}
B. 코드와 관련된 모든 중요한 것을 코드만으로 전달할수 있을까?
아니면 역시 주석은 나름대로의 가치가 있는 것일까?
특별한 케이스(무슨 이유때문에 코드를 다르게 작성했는지, 무엇을 막기 위해 주석을 그러한 방식으로 작성했는지에 대한)일때
는 주석이 필요하다.
특히 비슷한 모양, 이름의 메소드 클래스에서는 주석이 필요하다. (JAVADOC형식)
코드는 일반적으로 어떻게를 매우 잘 표현 할수 있는 반면, 왜에 대해서는 잘 표현하지 못하는 경우가 있다.
특히, 왜 안되는가에 대해서는 거의 표현이 불가능하다.
코드가 다른 사람들에 공개되면, 그 코드를 보는 사람들 중 일부는 추가 정보를 제공하는 JavaDoc 주석을 포함하는 것이 중요하다는
것을 발견하게 된다.
C. 최근에 작성한 코드를 살펴보자. 코드에 주석이 달려있을 가능성이 높다.
코드가 여러분의 의도를 더 직접적으로 반영하도록 만들어서 주석들 중 일부를 없앨수 있을까?
더보기
// 알리미 신청 되어있는 상태로 수정해야될때 ----------------------------------
if (state.equals(AlimyServlet.PARAM_VALUE_STATE_UPDATE)) {
String content = "회원님은 현재 ["
+ FortuneConstant.NEW_DAILY_SMS_ARRAY[(dailysms == 10 || dailysms == 11) ? SMSDeliver.SMS_MONTH
: dailysms] + "] 알리미 신청중입니다.";
if (isfree.equals(AlimyServlet.PARAM_VALUE_FREEVIEW_OK)) {
content += "매일 아침마다 알리미를 받으시고 오늘의 운세를 손쉽게 보세요~";
}
내용이 긴 블럭을 Extract Method를 실행.
if 분기문안의 내용을 줄여서 상단 주석을 없애도 문맥을 이해하도록 변경한다.
더보기
if (state.equals(AlimyServlet.PARAM_VALUE_STATE_UPDATE)) {
addUpContents(isfree, contentcss, dailysms);
initMenuList(contentcss);
addUpMenuItem(popup, isfree, disURL, dailysms);
}
private void addUpContents(String isfree, CSSData contentcss, int dailysms) {
String content = "회원님은 현재 ["
+ FortuneConstant.NEW_DAILY_SMS_ARRAY[(dailysms == 10 || dailysms == 11) ? SMSDeliver.SMS_MONTH
: dailysms] + "] 알리미 신청중입니다.";
if (isfree.equals(AlimyServlet.PARAM_VALUE_FREEVIEW_OK)) {
content += "매일 아침마다 알리미를 받으시고 오늘의 운세를 손쉽게 보세요~";
}
top_content = new CContent(content, contentcss);
}
private void addUpMenuItem(String popup, String isfree, String disURL,
int dailysms) {
if (popup.equals(AlimyServlet.PARAM_VALUE_POPUP_UP)) {
menuEnteredUpPopup(disURL);
} else if (isfree.equals(AlimyServlet.PARAM_VALUE_FREEVIEW_OK)) {
menuEnterdInsFree(disURL);
} else {
menuEnteredUpBasic(disURL, dailysms);
}
}