6
rant1ng
5y

nested ternary operators

like/dislike?

I used to hate them cuz I would have to break them apart just to understand them, but now I use ternary operators so much, nesting at least one level is ok for me.

but i'm the only person that reads my code, what's the concensus.. nesting one level bad?

I wouldn't want someone reviewing my code if they couldn't wrap their head around a simple ternary, so if only myself or people more experienced than myself will ever read them, then fuck it, i'm using them

Comments
  • 3
    Do you mean nested ternary expressions?
  • 3
    @HughRant err... yes. oops.
  • 5
    0-Level(normal ternary) can be bad.
    1-Level(ternary in ternary): bad
    2-level++: evil refined in each level.
  • 2
    @rant1ng Well, I wouldn’t mind as long as it’s readable. But I must challenge the idea that a nested ternary expression would ever be less convoluted than a series of nested if-blocks.
  • 1
    @HughRant

    To me they are readable...i user them mostly in return statements.. saves me from scrolling to read functions
  • 1
    functions. use static functions, or something more readable then trenary.
    I can live with runtime config flags in ternary, but not in actual code.
  • 3
    Bad. Ternary is meant to be plain simple. Not to hide 12 if statements.

    Okay, originally ternary had a slightly different purpose, but simplicity is very closely related to it.
  • 0
    Nested ternary in PHP. God forbid.
  • 1
    Please dont.
    Don't even joke about this shit.

    Ternary operators are fine if done well for a single condition, but nested? There's no way on this god forsaken earth you or the next dev will understand what the hell is going on without breaking it apart slowly later on.

    Also I've seen ternary with initiation and assignment so all of a sudden your little shit if() statement you couldn't be bothered writing correctly, now becomes an important piece of the puzzle.
  • 0
    If they're in parentheses? Tolerable; better with indenting.
    If they're not? The author deserves to be stabbed.
Add Comment