8
jdmng0
3y

string action = day == "Monday" ? "Go to work" : "Friday" ? "Netflix & chill" : "Saturday" ? "Sleep all day" : "Sunday" ? "Get ready again for Monday" : "Code from 9-5";

I wrote nested ternary operators before and I swear I'd only use shorter conditions like the one below. 😳

string action = isTired == true ? "Get some sleep" : "Drink more coffee";

Comments
  • 2
    Fuck that, ternary operators ftw! I love em
  • 1
    @iam13islucky Well, *nested* ternary operators aren't faster to read when debugging than to just use a simple if-else statement. I've been using ternary operators in my codes but I try to stick with shorter conditions.
  • 2
    I'd only use them for small things where it's obvious what's being done. Not for huge logic where switch or if statements would be a lot more readable and easy to debug. What's the point of writing short code and then spending hours going through it when something doesn't work because it isn't debuggable?
  • 2
    Nested ternaries aren't difficult to read if you add parenthesis around each.

    If you don't, they're annoying.
  • 0
    Always go for the readability :D
Add Comment