Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[20210404] int[] to String[], 매직 넘버(매직 리터럴) #87

Open
JuHyun419 opened this issue Apr 4, 2021 · 0 comments
Open

[20210404] int[] to String[], 매직 넘버(매직 리터럴) #87

JuHyun419 opened this issue Apr 4, 2021 · 0 comments
Labels

Comments

@JuHyun419
Copy link
Owner

JuHyun419 commented Apr 4, 2021

Converting int array to String array in java

  • Case1(Navie)
        // input primitive integer array
        int[] intArray = { 1, 2, 3, 4 ,5 };
 
        String strArray[] = new String[intArray.length];
 
        for (int i = 0; i < intArray.length; i++)
            strArray[i] = String.valueOf(intArray[i]);
 
        System.out.println(Arrays.toString(strArray));

  • Case2(Java 8 stream)
        String strArray[] = Arrays.stream(intArray)
                                .mapToObj(String::valueOf)
                                .toArray(String[]::new);

매직넘버(매직 리터럴)

  • 코드 내에 하드코딩 되어있는 숫자 혹은 문자열
  • 이를 정적(static) + 불변(final)로 선언해서 사용하자 !
  • Why ? 숫자 혹은 문자열로 정의되어 있으면 이 값이 무슨 뜻을 의미하는지, 코드 작성자가 아니면 이해할 수 없음
    • 시간이 지나면 코드 작성자도 이해할 수 없을듯 !!
  • 숫자 1을 ONE로 짓는 의미없는 상수 변환은 피할 것
// 3이라는 숫자가 어떤걸 의미하는지 코드 작성자가 아니면 바로 이해할 수 없음
if (splitArr.length == 3) {
    ...

// 수정 if문이 Manager이라는 것을 알 수 있음.
if (isManager(splitArr.length)) {
   ...

// isManager 메서드에 3이라는 숫자가 하드코딩 되어있어서, 이 값도 유의미하게 변경하는게 좋을듯..?(맞나 ?!)
private static boolean isManager(int length) {
    return length == 3;
}

// 예시 ..
private static boolean isManager(int length) {
    return length == MAX_MANAGER_LENGTH; 
}
@JuHyun419 JuHyun419 added the Java label Apr 4, 2021
@JuHyun419 JuHyun419 changed the title [20210404] int[] to String[] [20210404] int[] to String[], 매직 넘버(매직 리터럴) Apr 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant