hsunny study blog

1.5 Magic Number 본문

book/The Practice of Programming

1.5 Magic Number

헤써니 2015. 12. 24. 00:23

Magic number

Magic number ->

constants, array sizes, character positions, conversion factors,
and other literal numeric values that appear in programs.

Give names to magic numbers.

raw number in program source gives no indication of its importance or derivation, making the program harder tounderstand and modify.

Define numbers as constants, not macros.

in C: #define int MAXROW = 24. MAXCOL = 80;

in C++: const int MAXROW = 24. MAXCOL = 80;

in JAVA: static final int MAXROW = 24, MAXCOL = 80;

 

 

A related issue is that the number 0 appears often in programs, in many contexts.

So, don't write
? str = 0;
? name[i]=0;
? x=0;


but rather:
str = NULL;
name[il = '\0';
x = 0.0;
We prefer to use different explicit constants, reserving 0 for a literal integer zero,
because they indicate the use of the value and thus provide a bit of documentation.

 

 

W O R D

constant 정수

conversion factors

opaque 불투명한, 이해하기 힘든

derivation 어원

blunt 무딘, 뭉툭한

lexical 어휘의

underfoot 발밑에

 

 

'book > The Practice of Programming' 카테고리의 다른 글

1.6 Comment  (0) 2015.12.28
1.4 Function Macros  (0) 2015.12.23
1.3 Consistency and Idioms  (0) 2015.12.22
1.2 Expressions and Statements  (0) 2015.12.21
1.1 Names  (0) 2015.12.21