Is the ternary operator evil?

When I was reviewing the program of a newcomer who made his debut as a programmer, I got a glimpse of the ternary operator, so I will summarize it. To summarize first, the ternary operator is not an abbreviation for the if statement.

Postscript 2015/10/15 Here, "conditional operator" is written as "ternary operator". The ternary operator is a general term for operators that have three items as the name suggests, and does not directly refer to the "?:" Operation, so it was more accurate to describe it as the "conditional operator" here. Thank you bsdhack.

What is a ternary operator?

It is an operation that can switch the value to be returned depending on the condition.

Format


[conditions] ? [Return value if TRUE] : [Return value for FALSE] ;

sample


final int MAX = 100;
int ans = 120;

int result = MAX < ans? MAX : ans;
//result is 100

An example is the process of returning 100 if ans is 100 or more, and returning ans as it is if it is 100 or less.

The beginning of the matter

In the above example, there was source code like this.

if statement sample


int ans = 120;
int result;

if(MAX < ans){
  result = MAX;
}else{
  result = ans;
}

HM. I feel the same as the process. Rather, I feel that such source code is often used as an explanation for ternary operators. So why not use the ternary operator? When I heard that, he said, "I was taught not to use the ternary operator because it is difficult to understand." It is true that some companies and projects have rules that prohibit ternary operators.

No, it would be different to branch anything with if, so let's think about how to use it properly.

if "statement" and ternary "operator"

When considering if statements and ternary operators, the first thing to notice is the difference between "statements" and "operators". Since the if statement is a control syntax, it branches the process. On the other hand, the ternary operator is not for branching as it is named "operator".

If you are aware of that, I think it will be clear how to use it properly.

operator

Operators include "+" and "<=". If you think about the meaning again

ans = [Item 1] + [Item 2]

In that case, the "+" operator has the function of returning the sum of the numerical values of item 1 and item 2.

So the ternary operator is

ans = (conditions) ? [Item 1] : [Item 2]

Either item 1 or item 2 will be returned, and which one will be returned depends on the conditions. It becomes the function.

Use properly

The if statement is used to branch the process. The ternary operator is used when performing the process of selecting one from two (or more).

In the above example, since it is a process to select "MAX" or "as is", the ternary operator is suitable, and as a result, it is simpler and shorter than writing with an if statement. On the contrary, using the ternary operator because I want to write it short is a mess.

Cause of confusion

It is said that "ternary operators are not readable", and I think the reason why they are disliked is that the bad pattern of using ternary operators instead of if statements is widespread. If you write "process" in the ternary operator, you will be told that the if statement is fine.

Summary

This article was for the purpose of saying that the if statement and the ternary operator have different roles in the first place. If you clarify the role and use it properly, the readability should be improved because the intent of the code is communicated to the reader. However, I think that it is a difficult problem to draw a line between what is "calculation" and what is "branch processing" because the purpose of the program to be created and the sense of the programmer are greatly involved.

Recommended Posts

Is the ternary operator evil?
About the ternary operator
Try using || instead of the ternary operator
How do you write the ternary operator (? :)
Reference ternary operator
Assignment to multiple variables with the ternary operator
How much ternary operator is allowed in Java
What is an operator?
About the [ruby] operator
The migration file is duplicated
Isn't the While statement evil?
This is the first post.
What is the BufferedReader class?
What is the constructor for?
Until the code is executed
Java is the 5th day
What is the initialize method?
Kotlin has no ternary operator (conditional operator)
'% 02d' What is the percentage of% 2?
Java basic learning content 3 (operator / ternary operator)
Where is the Java LocalDateTime.now () timezone?
The Biostar Handbook is too interesting
How to write a ternary operator
[Ruby] I tried to diet the if statement code with the ternary operator
Write a third If statement that is neither an if statement nor a ternary operator