Devil numbers in programs

Source: Internet
Author: User
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;

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.