본문 바로가기
개발/기본) 개발용어

개발용어) 매직 넘버 (Magic Number)

by 테샤르 2021. 9. 6.

매직 넘버 (Magic Number)

프로그래밍에서 '매직 넘버'는 코드에서 직접 사용되는 숫자 값으로  '매직'이라는 용어는 코드를 읽는 사람에게 숫자의 의미가 즉시 명확하지 않고 왜 이 값이 들어갔는지 의미를 알기 힘든 수치값들이다.

반응형
        public static float WEAPON_ATTACK_DELAY_TIME_SEC = 1f;
        public static int REVIEW_PLAY_COUNT_INTERVAL = 5;
        public static int REVIEW_FIRST_PLAY_COUNT = 10;
        public static int REVIEW_CANCEL_COUNT = 2;
        public static float WEAPON_SCALE_VALUE = 1.5f;
        public static float CAMERA_ZOOM_DURATION = 0.5f;
        public static int GAME_PATH_FIND_COUNT_MAX = 300;
        public static float MAX_VALUE = 9999999f;
        public static float GAME_START_ZOOM_OUT = 3f;
        public static float GAME_PLAY_TIME_SEC = 30f;
        public static float GAME_PLAY_WARNING_TIME = 10f;
        public static int GAME_DEFAULT_CRITICAL_PERCENT= 25;
        public static int GAME_DEFAULT_CRITICAL_DAMAGE= 148;//
        public static int GAME_DEFAULT_COIN_MODE_RESULT_VALUE =100;
        
        
        
         case eCAMERA_STATE.RANK_1_ZOOM:
                    {
                        this.m_fUINickNamePlayWait = 0.25f;
                        this.UpdateCameraZoom(-30f, Define.CAMERA_ZOOM_DURATION * 3, () => { this.SetState(eCAMERA_STATE.RANK_1_ROTATION); });
                    }
                    break;

예시로 사용하는 코드에서 -30f라는 수치가 '매직 넘버'를 말한다.

매직넘버는 상황에 따라 종종 사용하게 된다.

예를 들면 특정 계산을 하기 위한 수식에 기준이 되는 가중치나 특정 정의된 수치 값들을 통칭해서 표현합니다.

대부분의 상수화된 수치는 정의 클래스( Define Class / Const Class )를 만들어서 사용한다.

반응형

 

또 다른 예시는 다음과 같다.

유닛의 체력을 정의하는 과정에서 수치값이 있을경우에는 다음을 권장한다.

int maxHealth = 100;
int currentHealth = 50;

수정된 코드를보면 좀더 명확하게 의미를 파악이 가능하다.

const int MAX_HEALTH = 100;
const int STARTING_HEALTH = 50;

int currentHealth = STARTING_HEALTH;

 

반응형

단순하게 수치를 각 코드마다 사용하다가 수정이 필요한 경우에 편의성과, 휴먼이슈, 사이드이펙트를 줄이기 위해서이다.

심볼릭 정수로 치환을 해서 작업하는 방향(replace magic number with symbolic constant)을 추천한다.

 

 

★★☆☆☆

 

반응형

댓글