본문 바로가기

Java

[Good Java Style -1] 탭 vs 스페이스 (Tab vs. space)


Tabvs.Spaces문제는코드를작성하는데관련된프로그래머개개인의신앙적인문제 (religiousissue)중하나이며,

나는하나의방법만이옳다고말하지않겠다.그러나빈칸 을사용하면내코드가내에디터에서보인것과똑같이당신의

에디터에서보인다는것 을보장할수있다.다만당신이탭대신에빈칸을사용하는것이적절하지않다고느낀다면

그냥탭을사용하시라.

주석

당신의 자바 코드에 삽입할 수 있는 주석은 두 가지 형태가 있다. 문서화 주석이라고도 불리는 Javadoc 주석과

구현(implementation) 주석이 그것이다. Javadoc 주석은 javadoc 툴로 추출하여 API 문서로 만들 수 있다.

구현 주석은 코드에 대한 "어떻게(how)""(why)"를 설명하는 주석이다. 당신의 자바 코드에 주석을 다는 데 다음의 지침을 사용하라.

o 가능할 때마다 Javadoc 주석을 사용하라. (최소한 클래스와 메서드들에 대해)

o 변수 선언과 같이 특별한 경우를 제외하고는, 줄 끝의 // 주석보다는 블록 주석 (/* */)을 사용하라.

또한 좋은 주석은 도움을 주지만 나쁜 주석은 귀찮고 방해가 된다는 것을 명심하라.

다음에 그러한 예를 보여준다. (역자 주. 원문을 살리기 위해 영문 주석을 그대로 살림)

E x a m p l e 1 . 나쁜 주석 스타일

// applyRotAscii() -- Apply ASCII ROT
private void applyRotAscii(){

try{
// get rot len
int rotLength = Integer.parseInt(rotationLengthField.getText().trim());
RotAscii cipher = new RotAscii(rotLength); // new cipher
textArea.setText(cipher.transform(textArea.getText())); // transform
}catch(Exception ex){
/* Show exception */
ExceptionDialog.show(this, "Invalid rotation length: ", ex); }
}

E x a m p l e 2 . 좋은 주석 스타일

/**
* Apply the ASCII rotation cipher to the user's text. The length is retrieved
* from the rotation length field, and the user's text is retrieved from the
* text area.
*
* @author Thornton Rose
*/

private void applyRotAscii() {
int rotLength = 0; // rotation length
RotAscii cipher = null; // ASCII rotation cipher

try {
// Get rotation length field and convert to integer.
rotLength = Integer.parseInt(rotatio nLengthField.getText().trim());

// Create ASCII rotation cipher and transform the user's text with it.
cipher = new RotAscii(rotLength);
textArea.setText(cipher.transform(textArea.getText()));
} catch(Exception ex) {
// Report the exception to the user.
ExceptionDialog.show(this, "Invalid rotation length: ", ex);
}
}