How do you write the ternary operator (? :)

It is argued that the ternary operator (? :) increases readability and decreases readability. I find it easy to read, depending on how I write it. Readability is a common story, so I'll write a story about how to actually get it in your head.

The ternary operator (? :) is easy to read but difficult to write?

When I told my juniors how to write a highly readable ternary operator (language is Java) "I looked it up before and it's certainly easy to see, but when I write it, I forget it. I look it up every time and think about it." So I talked about how I wrote the source when I wrote it. It seems that it was good as it is, so I will write it as an article.

I'm coding like this, but how about everyone?

Example: A plan if it is 10 or more, B plan if it is 5 or more, C plan if it is less than 5

There are various ways to write it, but as a coding style, for example, it is written like this.

String plan = count > 10 ? "A" :
              count > 5  ? "B" :
              /* other*/   "C" ; 

And

String plan = count > 10 ? "A" 
            : count > 5  ? "B"
            : /* other*/   "C" ; 

In the above writing, ":" is at the end of the line, and there is a feeling that "the condition still continues to the next line". I like it, but the writing style I often see is below.

Coding while chanting

I write while chanting with my head as follows. I'm chanting in parentheses in my head.

Once you get used to it, you will be able to use it naturally. If you repeat coding while chanting about 3 or 4 times, After that, the brain miso spends less time on the grammar of the ternary operator, You will be able to code naturally (I think ...).

(Plan) count> 10 (In the case of?) "A" (···so)
     count > 5  (In the case of?) "B" (···so)
(Otherwise)"C" ; 
Characters to actually write/Character to read Words chanted in the head
? In the case of?
: ···so,
/* other */ Otherwise,

Miscellaneous feelings

Recommended Posts

How do you write the ternary operator (? :)
How to write a ternary operator
About the ternary operator
Is the ternary operator evil?
When will you do the refactoring?
Try using || instead of the ternary operator
Assignment to multiple variables with the ternary operator
Reference ternary operator
[Java] Memo on how to write the source
How much ternary operator is allowed in Java
Do you really understand? How to check the library and license used by the app
[Even beginners can do it! ] How to write Javadoc
Java # 1: Write down the skills you learned every day
How to write Rails
[Ruby] Conditional expression without if? Meaning of (question mark). How to use the ternary operator.
Do you know Decorator?
How to write dockerfile
Do you need dotenv-rails?
How to write docker-compose
How to write Mockito
How to write migrationfile
About the [ruby] operator
How do you create foreign key values ​​in FactoryBot ~ (crying
How to replace characters that you do not understand [Principle]
[AWS / S3] After all, how do you upload multiple files?
What to do if you can't use the rails command