티스토리 뷰

대분류 - Portability [PORT]

규칙명 - Do not hard code '\n', or '\r' as a line separator [PORT.LNSP-1]

- 흔히 많은 개발자들이 메시지를 생성하기 위해 무심코 \n, \r 등을 사용한다.
허나. 이것 역시 특정 플랫폼에 종속적이기 때문에 사용하면 안된다.

DESCRIPTION

This rule identifies strings that contain hard coded '\n' or '\r' line separator characters. An error is reported for each occurrence.

'\n', '\r' 과 같은 플랫폼에 종속적인 개행문자를 사용하는 부분을 찾아낸다.
- 추가로 테스트는 못해봤지만, 개행문자 이외에 디렉토리 구분자('\') 또한 플랫폼마다 달라질 수 있으므로 다음 설명과 같이 System.getProperty()를 이용해서 사용해야 한다.

BENEFITS

Different systems use different characters or characters sequences as
line separators.  Consequently, hard coding '\n', '\r', or "\r\n" violates
the portability of Java code.

간단히 이식성(Portability)가 문제가 되기 때문에 '\n', '\r'등을 하드코딩하지 않는 것이 좋단 이야기 + 시스템에 종속적인 부분은 모조리 문제가 된다.

EXAMPLE

package examples.rules.port;

public class LNSP {
    public void sayHello (String name) {
        System.out.println("Hello\n" + name);  // VIOLATION
    }
}

이건 좀 억지가 있는 예제이긴 하지만, 어찌되었건 사용하는건 옳지 않다.

REPAIR

Use the println() method of PrintStream or PrintWriter, which automatically terminates a line with the line separator appropriate for the platform, or use the value of System.getProperty("line.separator").

package examples.rules.port;

public class LNSPFixed {
    public void sayHello (String name) {
        System.out.println("Hello");  // FIXED
        System.out.println(name);  // FIXED
    }
}

위의 예에서 처럼 System.getProperty() 를 이용해서 시스템 종속적인 값들을 얻어와야 한다.
다음은 System.getProperty()를 통해 얻어올 수 있는 값들 중 중요한 몇가지이다.

Key Description of Associated Value
java.version Java Runtime Environment version
java.vendor Java Runtime Environment vendor
java.vendor.url Java vendor URL
java.home Java installation directory
java.vm.specification.version Java Virtual Machine specification version
java.vm.specification.vendor Java Virtual Machine specification vendor
java.vm.specification.name Java Virtual Machine specification name
java.vm.version Java Virtual Machine implementation version
java.vm.vendor Java Virtual Machine implementation vendor
java.vm.name Java Virtual Machine implementation name
java.specification.version Java Runtime Environment specification version
java.specification.vendor Java Runtime Environment specification vendor
java.specification.name Java Runtime Environment specification name
java.class.version Java class format version number
java.class.path Java class path
java.library.path List of paths to search when loading libraries
java.io.tmpdir Default temp file path
java.compiler Name of JIT compiler to use
java.ext.dirs Path of extension directory or directories
os.name Operating system name
os.arch Operating system architecture
os.version Operating system version
file.separator File separator ("/" on UNIX)
path.separator Path separator (":" on UNIX)
line.separator Line separator ("\n" on UNIX)
user.name User's account name
user.home User's home directory
user.dir User's current working directory


REFERENCES

David Flanagan: "Java in a Nutshell".  O'Reilly,
November 1999: Third Edition, pp. 191-192


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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
글 보관함