Skip to main content

If the meaning of the number is very clear in the context, I don't think it's a "magic number" problem.

ExmapleExample: Let's say you're trying to get the substring of a string, from the beginingbeginning to some token and the code looks like this (imaginary language and library):

s := substring(big_string, 0, findFirstOccurence(SOME_TOKEN, big_string));

In this context, the meaning of the number 0 is clear enough. I suppose you could define START_OF_SUBSTRING and set it to 0, but in this case I think it would be overkill (although it would be the correct approach if you knew that the start of your substring might not be 0, but that depends on the specifics of your situation).

Another example might be if you are trying to determine if a number is even or odd. Writing:

isEven := x % 2;

is not as strange as:

TWO := 2;
isEven := x % TWO;

Testing negative numbers as

MINUS_ONE := -1;
isNegativeInt := i <= MINUS_ONE;

also feels weird to me, I'd much rather see

isNegativeInt := i <= -1;

If the meaning of the number is very clear in the context, I don't think it's a "magic number" problem.

Exmaple: Let's say you're trying to get the substring of a string, from the begining to some token and the code looks like this (imaginary language and library):

s := substring(big_string,0,findFirstOccurence(SOME_TOKEN,big_string));

In this context, the meaning of the number 0 is clear enough. I suppose you could define START_OF_SUBSTRING and set it to 0, but in this case I think it would be overkill (although it would be the correct approach if you knew that the start of your substring might not be 0, but that depends on the specifics of your situation).

Another example might be if you are trying to determine if a number is even or odd. Writing:

isEven := x % 2;

is not as strange as:

TWO := 2;
isEven := x % TWO;

Testing negative numbers as

MINUS_ONE := -1;
isNegativeInt := i <= MINUS_ONE;

also feels weird to me, I'd much rather see

isNegativeInt := i <= -1;

If the meaning of the number is very clear in the context, I don't think it's a "magic number" problem.

Example: Let's say you're trying to get the substring of a string, from the beginning to some token and the code looks like this (imaginary language and library):

s := substring(big_string, 0, findFirstOccurence(SOME_TOKEN, big_string));

In this context, the meaning of the number 0 is clear enough. I suppose you could define START_OF_SUBSTRING and set it to 0, but in this case I think it would be overkill (although it would be the correct approach if you knew that the start of your substring might not be 0, but that depends on the specifics of your situation).

Another example might be if you are trying to determine if a number is even or odd. Writing:

isEven := x % 2;

is not as strange as:

TWO := 2;
isEven := x % TWO;

Testing negative numbers as

MINUS_ONE := -1;
isNegativeInt := i <= MINUS_ONE;

also feels weird to me, I'd much rather see

isNegativeInt := i <= -1;
added 273 characters in body
Source Link

If the meaning of the number is very clear in the context, I don't think it's a "magic number" problem.

Exmaple: Let's say you're trying to get the substring of a string, from the begining to some token and the code looks like this (imaginary language and library):

s := substring(big_string,0,findFirstOccurence(SOME_TOKEN,big_string));

In this context, the meaning of the number 0 is clear enough. I suppose you could define START_OF_SUBSTRING and set it to 0, but in this case I think it would be overkill (although it would be the correct approach if you knew that the start of your substring might not be 0, but that depends on the specifics of your situation).

Another example might be if you are trying to determine if a number is even or odd. Writing:

isEven := x % 2;

is not as strange as:

TWO := 2;
isEven := x % TWO;

Testing negative numbers as

MINUS_ONE := -1;
isNegativeInt := i <= MINUS_ONE;

also feels weird to me, I'd much rather see

isNegativeInt := i <= -1;

If the meaning of the number is very clear in the context, I don't think it's a "magic number" problem.

Exmaple: Let's say you're trying to get the substring of a string, from the begining to some token and the code looks like this (imaginary language and library):

s := substring(big_string,0,findFirstOccurence(SOME_TOKEN,big_string));

In this context, the meaning of the number 0 is clear enough. I suppose you could define START_OF_SUBSTRING and set it to 0, but in this case I think it would be overkill.

Another example might be if you are trying to determine if a number is even or odd. Writing:

isEven := x % 2;

is not as strange as:

TWO := 2;
isEven := x % TWO;

If the meaning of the number is very clear in the context, I don't think it's a "magic number" problem.

Exmaple: Let's say you're trying to get the substring of a string, from the begining to some token and the code looks like this (imaginary language and library):

s := substring(big_string,0,findFirstOccurence(SOME_TOKEN,big_string));

In this context, the meaning of the number 0 is clear enough. I suppose you could define START_OF_SUBSTRING and set it to 0, but in this case I think it would be overkill (although it would be the correct approach if you knew that the start of your substring might not be 0, but that depends on the specifics of your situation).

Another example might be if you are trying to determine if a number is even or odd. Writing:

isEven := x % 2;

is not as strange as:

TWO := 2;
isEven := x % TWO;

Testing negative numbers as

MINUS_ONE := -1;
isNegativeInt := i <= MINUS_ONE;

also feels weird to me, I'd much rather see

isNegativeInt := i <= -1;
Source Link

If the meaning of the number is very clear in the context, I don't think it's a "magic number" problem.

Exmaple: Let's say you're trying to get the substring of a string, from the begining to some token and the code looks like this (imaginary language and library):

s := substring(big_string,0,findFirstOccurence(SOME_TOKEN,big_string));

In this context, the meaning of the number 0 is clear enough. I suppose you could define START_OF_SUBSTRING and set it to 0, but in this case I think it would be overkill.

Another example might be if you are trying to determine if a number is even or odd. Writing:

isEven := x % 2;

is not as strange as:

TWO := 2;
isEven := x % TWO;