You have a literal value with a special meaning.Create a constant, name it according to its meaning, and replace the preceding literal value with this constant.
Motivation: In computing science, magic number is one of the undesirable phenomena with a long history. The so-called magic number refers to a number that has a special meaning, but cannot clearly express this meaning. If you need to reference the same logical number in different locations, the magic number will upset you, because once these numbers change, you mustProgramAnd modify them all. Even if you don't need to modify it, you may have to worry about how to accurately point out the purpose of each magic number.
Constants can be declared in many languages. Constants do not cause any performance overhead, but they can be greatly improved.CodeReadability.
Before refactoring this item, you should look for other alternatives. You should observe whether the magic number is used, and then you will often find a better way to use it. If the magic number is a type code, use replace type code with class (replace the type code with class). If the magic number represents the length of an array, when traversing an array, use an array instead. length.
Practice: 1. Declare a constant and set its value to the original magic value.
2Find all reference points of this magic number.
3Check whether the new declared constant can be used to replace the magic number. If possible, replace it with this constant.
4, Compile.
5 compile and test all magic counts after they are replaced. At this time, the entire application runs normally, just as it has not been modified.