How to Elegantly Remove if-else in Your Java Code
original link: https://medium.com/@malvin.lok/how-to-elegantly-remove-if-else-in-your-java-code-27b2393544ae
practicing principles:
Find a sample article of the same type that you want to improve.
Translate it into Chinese.
Look at the Chinese translation and write it back in English.
Compare what you wrote with the sample essay, and upgrade your expression.
Practice in action. Select a relative topic and write in English.
My writing
Sometimes, that let if-else syntax be full of your code is not a correct way to let your colleagues to understand.
Sometimes, letting your code full of
if-elsestatement is not the most effective way to help your colleagues understand it.
Therefore, as a beginner, or even you are quite good at your job, still need to let your code more readable and (复杂?).
Thererfore, as a beginner, or even you are quite good at your job, you still need to make your code more readable and clear.
The first thing you should have a try is removing if-else statement(syntax) in your code. Here are some suggestions.
Choice 1: Earlier Early return
earlier return is grammatically correct but might be less common.
assume we have such code below:
Assume we have the following code:
public boolean isValid(String condition) {
boolean result;
if (condition != null){
if (condition.equals("hi") {
result = true;
} else {
result = false;
}
} else {
result= false;
}
return result;
}
This is what we usually used to return earlier, remove unnecessary else.
This is the kind of code that we generally use to return early, removing the unnecessary
else.
public boolean isValid(String condition) {
if (condition == null) {
return false;
}
if (condition.equals("hi") {
return true
}
return false;
}
This choice usually fits for simple code structure, we can return earlier, remove some of improper if-else
This approach is typically suitable for simple code structures; we can return earlier, removing some of the improper
if-elsestatements.This approach is generally only suitable for simple structures and we can return early to get rid of some of the unnecessary
if-else.
Choice 2: Enumeration
Assume that we have such a code snippet :
public String getLabel(int status) {
String label;
if (1 == status) {
label = "Padding";
} else if (2 == status) {
label = "Paid";
} else if (3 == status) {
label = "Success";
} else if (4 == status) {
label = "Failed";
}
return label;
}
Maybe you should say that there is no one write code like this in 20th century. But actually, code snippet like this is very common.
Perhaps you could say, no one writes code like this in the 20th century. However, in reality, code snippets like this remain very common.
** A code snippet like this can be replaced by an Enumeration perfectly, let’s define an Enum class at first:
This type of code is well-suited for enumeration to solve, let’s define an enumeration first:
@Getter
@AllArgsConstructor
public enum StatusLabelEnum {
Padding(1, "Padding"),
Paid(2, "Paid"),
Success(3, "Success"),
Failed(4, "Failed"),
;
private int status;
private String label;
public static String getLabelByStatus(int status) {
for (StatusLabelEnum labelEnum : StatusLabelEnum.values()) {
if (labelEnum.getStatus() == status) {
return labelEnum.getLabel();
}
}
return "Unknown";
}
}
When we have an Enum class, method getLable() can be optimized to only just one line of implementation:
public String getLabel(int status) {
return StatusLabelEnum.getLabelByStatus(status);
}
Certainly, in a actually project, this choice is not the best but is to set a key-value config table in database.
Certainly, in an actual project, this choice is not the best but is to set a
key-valueconfig table in the database.
If the state is stable, Enumeration still is a simple solution.
If the state is stable, Enumeration remains a simple solution.
Choice 3: Optional
I’m very sure that you guys’s projects have a not-null judge. When it goes to be null, it will be returned or throw a exception:
I’m very sure that you guys have projects where there is a non-null check. and if it’s null, it will be returned or throw an exception:
public int getOrderStatus(UUID id) {
Order order = getOrderById(id);
if (order == null) {
return 1;
} else {
return order.getOrderStatus();
}
}
We can use Optional elegantly to solve this problem.
We can elegantly use
Optionalto slove this problem.
public int getOrderStatus(UUID id) {
Order order = getOrderById(id);
return Optional.ofNullable(order).map(Order::getOrderStatus).orElse(1);
}
The source link has 4 ways to help you to remove if-else in your code. But this article will only present you the first 3 ways, as for the reason, I would not tell you that it because I don’t want to write about it.
The source link mentions four methods, but this article only covers three of them. As for the reason, I won’t tell you it’s because I don’t want to write about it.