In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the ways to reduce ifelse coding habits in java". In daily operation, I believe that many people have doubts about the methods of reducing ifelse coding habits in java. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the ways to reduce ifelse coding habits in java?" Next, please follow the editor to study!
Method 1: return in advance
First, show an example of a code:
If (condition1) {if (condition2) {return getSomething ();} else {return 0;}} else {return 0;}
The modified code is as follows:
/ / it is best to describe the logical supplementary comments judged by this flag boolean flag =! condition1 | | (condition1 & & condition2) if (flag) {return 0;} if (condition1 & & condition2) {return getSomething ();}
If there is a logic that is known to return a fixed value under certain conditions, this part of the logic can be extracted as an independent if-else block and placed in front of other if-else block. When the specific condition is met, the return fixed value is directly advanced. The most direct effect of this approach is to reduce the number of nesting if-else.
Method 2: use the ternary operator
Let's start with an example. Here's a business scenario:
Query a comment's picture URL list (if any, the comment's picture url list is saved in the comment table in the JSON array string format)
The code before modification is as follows:
Comment comment = getById (commentId); if (Objects.isNull (comment)) {throw new RuntimeException ("comment does not exist or has been deleted");} String imgListStr = comment.getImgList (); if (StringUtils.isEmpty (imgListStr)) {return null;} return JSON.parseArray (imgListStr, String.class)
After modification:
Comment comment = getById (commentId); if (Objects.isNull (comment)) {throw new RuntimeException ("comment does not exist or has been deleted");} String imgListStr = comment.getImgList (); return StringUtils.isEmpty (imgListStr)? Null: JSON.parseArray (imgListStr, String.class)
Method 3: use Assert assertions
In the process of writing business code, if some specific conditions need to be judged, and an exception needs to be thrown when the conditions are not satisfied. For this scenario, in addition to using the if method in the example of the ternary operator above, you can also use the Assert utility class provided to us by Spring Framework. The commonly used API are:
IsTrue (boolean expression, String message): an exception is thrown when expressio = = false, and the message of the exception is the second input parameter; void notNull (@ Nullable Object object, String message): as above, an exception is thrown when object = = null; void notEmpty (@ Nullable Collection collection, String message): as above, an exception is thrown when the collection object is null or the collection element is empty. .
There are many other methods, you can directly look at the source code parsing, of course, isTrue () is actually enough, if you need more semantics, you can use the corresponding API.
Code before modification:
If (Objects.isNull (comment)) {throw new RuntimeException ("comment does not exist or has been deleted");}
Modified code:
Assert.isTrue (Objects.nonNull (comment), "comment does not exist or has been deleted"); Assert.notNull (comment, "comment does not exist or has been deleted")
At present, the Assert tool method can only throw a single exception java.lang.IllegalArgumentException, which is not applicable if you need to customize the exception thrown.
Method 4: use Optional
Optional is a new feature of java8, which is equivalent to an object's container. It is mainly used to check the null value of an object, and to chain subsequent operations after checking, such as throwing an exception, null replacement and so on.
Some of the methods that I personally use are as follows:
Static Optional ofNullable (T value): wraps the object with Optional; T orElse (T other): returns the object that enters the parameter when the object in Optional is null. T orElseGet (Supplier
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: 251
*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.