Spit it out. Code in the project bad taste: Abusing Java constants

Source: Internet
Author: User

Are our projects flooded with code like the following? Defines a Java class (interface) that specifically holds constants, and many other classes rely on that constant class.

public interface Iconstant{int ZERO = 0; String empty_string = "";}

The code that uses the constant is roughly the following form:

list<string> list = new Arraylist<string> (Iconstant.zero), if (Iconstant.zero = = List.size ()) {}int[] array = {1,2,3}array[iconstant.zero];if (IConstant.EMPTY_STRING.equals (name)) {}

Why do you define 0 and "" as a constant? It is not correct to understand exactly what is the Devil number and hard coding. Constants are used for several reasons:

1. For readability, a good name is clearly easier to understand than a string of dead numbers.

2, avoid redundancy, so that the code easier to modify, to achieve a place, everywhere the effect.

3, avoid the programmer handwriting constants, careless write wrong. For example, write a 0 or less write a 0.

4, constants must be used in multiple places, but also in different places to represent the same meaning.

If the defined constants do not meet any of these 4 requirements, then it can be said that the use of this constant is meaningless, resulting in another code bad taste: There is no necessary complexity.

In our example above: 0 represents the length of the list and also represents the first element of the array. Does not meet the 1th requirement, because for programmers, this is only the most basic syntax, the additional definition of variables can not improve readability, not meet the 2nd requirement, because these things will not change, there is no possibility of modification, do not meet the 3rd requirement, there is the IDE's compilation hint, and the constant 0 is not complex, there is no possibility of error ; does not meet the 4th requirement, because 0 minutes represents the length, while representing the array index, there is no way to give 0 a meaningful name.


4th need special attention: A constant name can only represent a business meaning! It is very bad practice to have the list length and array index "reuse" the constant value 0. Although the number of constants is reduced, it seems that the "reuse" is implemented, which actually increases the coupling between the code. As an extreme example, if Java syntax changes significantly, the first element of the array starts at 1 instead of 0, can we change the constant zero to 1? Obviously not, if modified, is able to guarantee the correct use of the array, but will cause the list length judgment error. In this scenario, the constant zero we define is completely meaningless.

Spit it out. Code in the project bad taste: Abusing Java constants

Related Article

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.