Demon numbers in the program

Source: Internet
Author: User

Using the Devil Numbers in your code (numbers, strings, etc. that don't have a detailed meaning) will cause your code to be difficult to understand and you should define a number as a constant with a meaningful name.

The last goal of defining a number as a constant is to make the code easier to understand, so it's not just the Devil's number to define a number as a constant. Assuming that the name of a constant is meaningless and does not help to understand the code, the same is a demon number.

In some cases, defining a number as a constant will cause the code to be more difficult to understand, and it should not be forced to define a number as a constant.

Case

Devil number, can't understand. 3 What is the status of the product in detail?

if (Product.getproduct (). Getproductstatus ()! = 3)

{

throw new Pmsexception (PMSErrorCode.Product.ADD_ERROR);

}

is still the devil number, unable to understand what the status of Num_three in detail represents the product

if (Product.getproduct (). Getproductstatus ()! = Num_three)

{

throw new Pmsexception (PMSErrorCode.Product.ADD_ERROR);

}

In the example, the code is not easy to understand, although the number is defined as a constant

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;

Using numbers directly, the code is 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;

Demon numbers in the program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.