In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.