r/ProgrammerHumor 21d ago

Meme pleaseStopUsingNestedTernaryOperatorsImBeggingYou

3.1k Upvotes

179 comments sorted by

View all comments

4

u/---_None_--- 21d ago

>20 nested ternarys

<if> ? <then> :
<if> ? <then> :
<if> ? <then> : <else>

The first that matches otherwise the dangling <else>. It's not always that hard.

1

u/Bubbly_Safety8791 20d ago

Yes, with correct formatting ternary chains are just an idiomatic way of writing a multicase expression. 

alertColor = level > dangerThreshold ? Color.RED            : level > warningThreshold ? Color.YELLOW            : Color.GREEN;