Devil numbers in programs

Source: Internet
Author: User

The use of devil numbers (numbers without specific meanings, strings, etc.) in the Code will make the code difficult to understand, and the number should be defined as a meaningful constant of the name.

The ultimate goal of defining a number as a constant is to make the code easier to understand, so it is not a devil number to define a number as a constant. If the constant name is meaningless, it cannot help you understand the code. It is also 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?

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 kind of status num_three represents.

If (product. getproduct (). getproductstatus ()! = Num_three)

{

Throw new pmsexception (pmserrorcode. Product. add_error );

}

// Although the number is defined as a constant in the example, 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;

Source Free qcoin resources: http://blog.sina.com.cn/hksqbi

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.