DEV Community

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

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.