Using devil numbers (numbers without specific meanings, strings, and so on) in the Code will make the code hard to understand. The numbers should be defined as constants with meaningful names.
The ultimate goal of defining a number as a constant is to make the code easier to understand, so it is not just to define a number as a constant that is not a devil number. Assume that the constant name is meaningless and cannot help you understand the code. The same is a devil number.
In some cases, defining a number as a constant makes the code more difficult to understand. In this case, you should not forcibly define the number as a constant.
Case
// The Devil's number cannot be understood. 3. What is the status of the product in detail?
If (product. getproduct (). getproductstatus ()! = 3)
{
Throw new pmsexception (pmserrorcode. Product. add_error );
}
// It is still the devil's number, and it cannot be understood what the num_three details represent the status of the Product
If (product. getproduct (). getproductstatus ()! = Num_three)
{
Throw new pmsexception (pmserrorcode. Product. add_error );
}
// In the example, although the number is defined as a constant, the Code is not easy to understand.
Point drawcenter = new point ();
Drawcenter. x = parentwindow. x + (parentwindow. Width-clientwindow. width)/half_size_div;
Drawcenter. Y = parentwindow. Y + (parentwindow. Height-clientwindow. Height)/half_size_div;
Return drawcenter;
// Directly use numbers to make the code easier to understand
Point drawcenter = new point ();
Drawcenter. x = parentwindow. x + (parentwindow. Width-clientwindow. width)/2;
Drawcenter. Y = parentwindow. Y + (parentwindow. Height-clientwindow. Height)/2;
Return drawcenter;