Magic numbers in formulas

This is a well-established convention that magic numbers should be avoided. But what about magic numbers in complex conditional formulas? For instance:

int result = 0;
if (level <= 50) {
    result = (int) (Math.pow(level, 3) * (100 - level) / 50);
}
else if (level <= 68 && level > 50) {
    result = (int) (Math.pow(level, 3) * (150 - level) / 100);
}
else if (level <= 98 && level > 68) {
    result = (int) (Math.pow(level, 3) * ((1911 - 10 * level) / 3) / 500);
}
else if (level < 100 && level > 98) {
    result = (int) (Math.pow(level, 3) * (160 - level) / 100);
}
return result;

In this case, it would be better to just say "avoid magic numbers when possible"? I also use CheckStyle in eclipse to show me where I might have missed the real magic number. However, it is not possible to disable the verification of some numbers, not others.

+4
source share
7 answers

I think that all magic numbers should be declared as constants or correctly documented.

A declaration of documentation is preferred.

Otherwise, the developer is not clear what the numbers are and why they are there.

+5

, . , ((1911 - 10 * level) / 3) / 500)? . ?

, , . ?

+2

.

int l3 = level * level * level;

return level <= 50 ? l3 * (100 - level) / 50 :
        level <= 68 ? l3 * (150 - level) / 100 :
                level <= 98 ? l3 * ((1911 - 10 * level) / 3) / 500 :
                        level < 100 ? l3 * (160 - level) / 100 : 0;

, 0, > 100.

, , , , , , .

0

. : , , , , , . , :

0 if x < 28 else 1

, , ", ". , 28, , , , .

, , , , .

/ , , :

var age  = 52; // my age
var code = 52; // my country long distance code

, , , , , . , - , , . 53 :

a = 52 * x + y + z + 52;

- ? , ,

a = 51 * x + y + z + 52;

, , , 52 !

0

- : ( , / )

int d = 1;
    int x = 3;
    int l = 1;
    int f = 1;
    int y = 0;

    if (level <= 50) {
        y = 100;
        d = 50;
    } else if (level <= 68 && level > 50) {
        y = 150;
        d = 100;
    } else if (level <= 98 && level > 68) {
        y = 1911;
        l = 10;
        f = 500;
        d=3;
    } else if (level < 100 && level > 98) {
        y = 160;
        d = 100;
    }
    return (int) (Math.pow(level, x) * ((y - level * l) / d) / f);
0

, ,

. ( , if, , ), , , ,

public int calcResult(int arg1, int arg2, int arg3)
{
   return (int)(Math.pow(level, arg1) * (arg2 - level) / arg3);
}

.... 

int result = 0;
if (level <= 50) {
    result = calcResult(3,100,50); // these can be replaced with constants.
}
else if (level <= 68 && level > 50) {
    result = calcResult(3, 150, 100);
}
else if (level <= 98 && level > 68) {
    result = (int) (Math.pow(level, 3) * ((1911 - 10 * level) / 3) / 500); // You can also change this accordingly.
}
else if (level < 100 && level > 98) {
    result = calcResult(3, 160, 100);
}
return result;

, . .

0

- , . - . , , . , .

, . - :

final int ONE = 1; // as if the value of 1 is going to change in the future

:

final int BITS_TO_SHIFT = 1;
// and further down in the code
int shifted_value = value >> BITS_TO_SHIFT;

, , :

  • :

    final int CUTOFF_1 =  50;
    final int CUTOFF_2 =  68;
    final int CUTOFF_3 =  98;
    final int MAX      = 100;
    
    
    int result = 0;
    if (level <= CUTOFF_1) {
        result = (int) (Math.pow(level, 3) * (100 - level) / 50);
    }
    else if (level <= CUTOFF_2 && level > CUTOFF_1) {
        result = (int) (Math.pow(level, 3) * (150 - level) / 100);
    }
    else if (level <= CUTOFF_3 && level > CUTOFF_2) {
        result = (int) (Math.pow(level, 3) * ((1911 - 10 * level) / 3) / 500);
    }
    else if (level < MAX && level > CUTOFF_3) {
        result = (int) (Math.pow(level, 3) * (160 - level) / 100);
    }
    return result;
    
  • , . . , . , , , .

Note that as a stranger, I see these magic numbers as a set of arbitrary numbers. Isn't it better to create constants with the corresponding names that describe the purpose of the constant?

0
source

Source: https://habr.com/ru/post/1548439/


All Articles