In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the solutions to optimize if-else", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the solutions for optimizing if-else"?
Optimization plan 1: advance return to remove unnecessary else
If the if-else code block contains return statements, consider getting rid of the excess else by return in advance to make the code more elegant.
Before optimization:
If (condition) {/ / doSomething} else {return;}
After optimization:
If (! condition) {return;} / / doSomething
Optimization scheme 2: using conditional trinomial operators
Using the conditional trinomial operator can simplify some if-else and make the code more concise and readable.
Before optimization:
Int price; if (condition) {price = 80;} else {price = 100;}
After optimization:
Int price = condition?80:100
Optimization scenario 3: using enumerations
In some cases, enumerations can also be used to optimize if-else logical branches, which can also be seen as a table-driven approach, according to personal understanding.
Before optimization:
String OrderStatusDes; if (orderStatus==0) {OrderStatusDes = "order not paid";} else if (OrderStatus==1) {OrderStatusDes = "order paid";} else if (OrderStatus==2) {OrderStatusDes = "shipped";}.
After optimization: (define an enumeration first)
Public enum OrderStatusEnum {UN_PAID (0, "order not paid"), PAIDED (1, "order paid"), SENDED (2, "shipped"),; private int index; private String desc; public int getIndex () {return index;} public String getDesc () {return desc;} OrderStatusEnum (int index, String desc) {this.index = index This.desc = desc;} OrderStatusEnum of (int orderStatus) {for (OrderStatusEnum temp: OrderStatusEnum.values ()) {if (temp.getIndex () = = orderStatus) {return temp;}} return null;}}
With the enumeration, the above if-else logic branches can be optimized to one line of code:
String OrderStatusDes = OrderStatusEnum.0f (orderStatus). GetDesc ()
Optimization scenario 4: merge conditional expressions
If you have a series of conditions that return the same result, you can combine them into a conditional expression to make the logic clearer.
Before optimization:
Double getVipDiscount () {if (age
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.