Hacker News new | past | comments | ask | show | jobs | submit login

Nested ternary is fine as long as it’s written on multiple lines because then it exactly mirrors the if/else-if/else structure.

    return (
        a < b ?
        -1 :
        a > b ?
        1 :
        0)



>then it exactly mirrors the if/else-if/else structure.

But it doesn't mirror the if/else-if/else structure. Rather the mirrored semantics looks as follows:

  if(a < b)
    return -1;
  else 
    if(a > b)
      return 1;
    else
      return 0;
It's interesting that for all the claims in this thread about how obvious the ternary operator is, almost every single person got it wrong.

Is there a functional difference in this example? No. There isn't. Is there a functional difference in any example? I don't think so, but I'm not 100% sure. And the nice thing about not being clever, I don't have to worry about it.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: