For example, would you prefer this one-liner

int median(int a, int b, int c) {
    return (a<b) ? (b<c) ? b : (a<c) ? c : a : (a<c) ? a : (b<c) ? c : b;
}


or an if/else solution involving multiple return statements?

When is ?: appropriate, and when is it not? Should it be taught to or hidden from beginners?


  Is the ternary operator evil?


No it's a blessing.


  When is ?: appropriate?


When it's something so simple you don't want to waste many lines for.


  and when is it not?


When the readability and clearness of code suffers and a potential for a mistake through insufficient attention increases, for example, with many chained operators, just like in your example.



The litmus test is when you begin to doubt your code is easily readable and maintainable in the long run. Then don't do it.
