DEV Community

Discussion on: Stop! Put Down That Ternary, Lines Are Free

Collapse
thumbone profile image
Bernd Wechner

I'm a full on proponent of coding for clarity rather than brevity or even efficiency and have lobbied for that for decades, and refactored plenty of code to that effect. But I am equally fond of and a proponent of prudent use of ternaries ... encapsulating a single idea completely and elegantly aids comprehension and clarity IMHO.

Collapse
allthecode profile image
Simon Barker Author

Totally, not against ternaries - I just feel that have become a hammer for some people and being used too liberally 😀

Collapse
thumbone profile image
Bernd Wechner • Edited on

Admit I don't read a lot of foreign code so don't know, and to be honest even if I did (read a lot of foreign code) I would be very wary of sampling bias before I drew any broad industry wide conclusions. But for my part I love this construct and use it a lot:

result = condition1 ? value1
       : condition2 ? value2
       : condition3 ? value3
       : condition4 ? value4
       : defaultvalue;
Enter fullscreen mode Exit fullscreen mode

It is extremely clear and uncluttered when used appropriately and easy to read.