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.
source
share