How much ternary operator is allowed in Java

Introduction

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 **.

What is a ternary operator?

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.";

What is the standard

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.

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.

How to write

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);

in conclusion

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

How much ternary operator is allowed in Java
Java basic learning content 3 (operator / ternary operator)
How slow is a Java Scanner?
How to learn JAVA in 7 days
How to write a ternary operator
How to use classes in Java?
How to concatenate strings in java
What is a class in Java language (3 /?)
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How do you write the ternary operator (? :)
How to do base conversion in Java
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
How to implement coding conventions in Java
What is the main method in Java?
How to get the date in java
How to display a web page in Java
The intersection type introduced in Java 10 is amazing (?)
How to get Class from Element in Java
How to hide null fields in response in Java
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
[Java] Something is displayed as "-0.0" in the output
How to write Java String # getBytes in Kotlin?
What is java
Changes in Java 11
Reference ternary operator
Rock-paper-scissors in Java
What is Java <>?
What is Java
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
[Java] How to omit the private constructor in Lombok
How to figure out how much disk Docker is using
How to input / output IBM mainframe files in Java?
Which is better, Kotlin or Java in the future?
How to create a data URI (base64) in Java
How to generate / verify ID token in Java Memo
How to convert a file to a byte array in Java
How to Git manage Java EE projects in Eclipse
In 2021, there is no topic in Java these days (Poem)
Summary of how to implement default arguments in Java
Java11: Run Java code in a single file as is
How to put old Java (8 series) in macOS 10.15 Catalina
Notes on how to use regular expressions in Java
The story that .java is also built in Unity 2018