Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

closed as not constructive by gnat, Walter, Thomas Owens Nov 26 '12 at 16:39

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

212  
This particular use of it is :) – karmajunkie Dec 20 '10 at 16:48
5  
Who coded that, and what does their version of a median for four numbers look like? Or five? – Mason Wheeler Dec 20 '10 at 21:56
3  
The more correct name is 'conditional operator'. It just happens to be the most common ternary operator in use. – Alan Pearce Dec 21 '10 at 15:11
1  
This was asked over two years ago on stackoverflow. Are we going to re-ask everything over here now? stackoverflow.com/questions/160218/to-ternary-or-not-to-ternary – webbiedave Dec 21 '10 at 16:30
3  
I am surprised why such questions keep coming up. The answer is always "whatever works and is readable." --the last one being equally important. – Apoorv Khurasia Nov 26 '12 at 6:18

25 Answers 25

Not the answer you're looking for? Browse other questions tagged or ask your own question.