This article is limited to Java.
The other day, I wrote the following article (I changed the title).
In this article, I've given the ternary operator as a good example:
String userType = isAdmin ? "Administrator" : "General";
However, there were quite a few reactions that the ternary operator was difficult to read. I intended to give it as a simple example, but I didn't explain it enough, so I will write a separate article.
My conclusion is that ** ternary operators can only be used for simple things **.
The ternary operator has the following form:
<Conditional expression> ? <Value when true> : <Value when false>
Before talking about which way to write, I will write an example of how much I feel "how much".
/*
*White(Almost no problem)
*/
String userType = isAdmin ? "Administrator" : "General";
String userType = user.isAdmin() ? "Administrator" : "General";
UserType userType = user.isAdmin() ? UserType.ADMIN : UserType.NORMAL;
String userName = user.isAdmin() ? "Administrator" : user.getName();
/*
*Gray close to white
*/
return user.getType() == UserType.GUEST ? "Guest user" : user.getName();
/*
*Gray close to black
*/
String userName = user.getType() == UserType.GUEST ? "Guest user" : user.getName();
/*
*black(out)
*/
String userName = user.getType() == UserType.GUEST ? "The guests" : user.getType() == UserType.ADMIN ? "Administrator" : user.getName() + "Mr.";
Return to the definition of the ternary operator.
<Conditional expression> ? <Value when true> : <Value when false>
In the previous example, what kind of pattern is good or bad can be classified as follows.
==
or ! =
The reason why ternary operators are often disliked is that the cost of lexical analysis and grammatical analysis in the head is high, such as how much is a conditional expression and how much is a value when true. Also, the reason why comparisons with ==
and ! =
Is not good is that they are mistaken for the substitution =
and are confused for a moment.
Therefore, I try to avoid anything other than simple writing.
You could write something like this, but I don't like it because it's a negative solution.
String userName; //Initialization here is useless, so don't
if (user.getType() == UserType.GUEST) {
userName = "The guests";
} else if (user.userType() == UserType.ADMIN) {
userName = "Administrator";
} else {
userName = user.getName() + "Mr.";
}
If you are yourself, create a method as follows. In this case, it can be implemented as a method of User class.
This time, I made an if statement to match the original conditional expression, but in the case of enum, I often use a switch statement.
private String getName(User user) {
if (user.getType() = UserType.GUEST) {
return "The guests";
} else if (user.getType() == UserType.ADMIN) {
return "Administrator";
} else {
return user.getName() + "Mr.";
}
}
//Caller
String userName = getName(user);
There were more patterns that were harder to read than I expected. I think it's overkill to ban ternary operators, but I think it's okay to use only simple ones.
Recommended Posts