Enumeration is very easy to use! Why doesn’t Ali recommend enumerating return values??

Time:2021-10-16

Source: zhihu.com/question/52760637

put questions to

The little partner said that enumeration was used in an interface definition, which was deeply disliked by others and difficult to expand.

Why is it rejected? Let’s take a look at the suggestions on enumerations in Alibaba’s development manual

It can be seen from the manual that the Alibaba development manual supports the definition and use of enumeration, but why should the return value be opposed?

Look at what the author Gu Quan said

Due to the upgrade, the enumeration classes of both sides are different, and an exception occurs during interface resolution and class deserialization. Any element appearing in Java will have thinking and logic behind it from the perspective of gosling (although it is not absolutely perfect, the top-level abstraction of Java is already genius), such as interfaces, abstract classes, annotations, and enumerations mentioned in this article.

Enumeration is good, type safe, clear and direct. You can also use the equal sign to judge, or it can be used in switch.

Its disadvantage is also obvious, that is, it can not be expanded. However, why is the difference between return values and parameters? If they are incompatible, there are problems with both. How can parameters be enumerated. At that time, if the parameters can not be used, then enumeration is almost useless.

After all, the parameter output is determined locally. If you have it locally, there will be no problem with forward compatibility. However, if it is returned by the interface, it will be disgusting, because the parsed enumeration value may not be available locally. At this time, a serialization exception will be thrown.

For example, your local enumeration class has a weather enum: sunny, rain and cloudy. If the method of calculating mood based on weather is guess (weatherenum XX), you can pass in these three values. Return value: weather guess (parameter). After the opposite operation, a snow is returned. There is no such value in the local enumeration. It’s silly.

However, another netizen Brian’s answer is also very easy to understand

Enumeration is to list all the known. As a provider of two-party / three-party libraries, you use whatever I support. This is safe. After the library version is upgraded, I support more. You won’t use it if you don’t know. Anyway, you can’t pass the parameters I don’t support to me, so enumeration is a security guarantee as input. But as a return value, the opposite is true.

I’ll tell you first that these can be, and then you stipulate that these can be, but nothing else.

But I has the final say, not you, so your rule is not. If you don’t read every word in the manual (if it’s fake), you don’t know what the upgraded API will return, and the possibility of throwing exceptions is 100%.

Recent hot article recommendations:

1.1000 + java interview questions and answers (2021 latest version)

2.Stop playing if / else on the full screen. Try the strategy mode. It’s really fragrant!!

3.what the fuck! What is the new syntax of XX ≠ null in Java?

4.Spring boot 2.5 heavy release, dark mode is too explosive!

5.Java development manual (Songshan version) is the latest release. Download it quickly!

Feel good, don’t forget to like + forward!

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]