Java programming skills: summary of if else Optimization Practice

Wen / Zhu Jiqian

To tell you the truth, I hate to use if else in my code. Firstly, the execution method of this kind of code is process oriented, and secondly, it will appear that the code is too redundant. This note mainly records some of my optimization experience for if else in my work practice, which will be updated for a long time from time to time.

1、 Use policy enumeration to optimize if else

Seeing that many people on the Internet recommend using the policy mode to optimize if else, I always think that it is too heavy to create a lot of policy classes to optimize a large number of if else. Although the idea is very good, it is likely to create many class objects inadvertently. If you want to use the policy mode to optimize a large number of if else, there is actually a better way. This is the improvement of the policy mode + enumeration method. I have written such an optimization article before. For details, click the article directly to learn: Policy enumeration: eliminating the elegant posture of using if else in a large number of projects

About the principle of immutable map, I specially wrote an article to introduce: Java source code analysis: source code analysis of immutable set immutable map of guava

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>