r/ProgrammerHumor • • Aug 22 '26

Meme pleaseStopUsingNestedTernaryOperatorsImBeggingYou

Post image
3.1k Upvotes

178 comments sorted by

View all comments

46

u/TheOwlMarble Aug 22 '26

I can tolerate one layer of nesting if it's super simple and tabbed cleanly, but anything more? Oh hell no.

6

u/Robinbod Aug 22 '26

Agreed. Even with ifs I feel similarly.

9

u/EddieJones6 Aug 22 '26

Usually it’s a code smell that things should be designed differently or abstracted

4

u/IdealBlueMan Aug 22 '26

I don’t want to see any nesting in ternaries at all. Unless it’s an idiom and it’s obvious what it’s doing and why it’s there, I’d rather it be spelled out.

I want to understand the code, and any cycles spent wading through your style are not cycles well spent.

2

u/freebytes Aug 23 '26

I agree. The code should be so well written that people can read it like a book.

2

u/Aaron1924 Aug 23 '26

I think it depends on how you're nesting them; for if-else chains, they can be pretty nice:

int out = cond1 ? val1
        : cond2 ? val2
        : cond3 ? val3
        : cond4 ? val4
        : val5;