views:

225

answers:

7

Anyone have a good trick to remember the standard ternary syntax?

Specifically whether the '?' or ':' comes first. I have consistently gotten this backwards over the years.

+13  A: 

The condition you are checking is kind of like a question, so the question mark comes first.

x > 0 ? 1 : 0

Think of this statement as three English sentences: "Is x greater than 0? Then 1. Else, 0." You have one sentence for each clause in the statement.

The predicate:

x > 0 ? /* Is x greater than 0? */

The "true" branch:

1 /* Then 1. */

The "false" branch:

: 0 /* Else, 0. */
A. Levy
There's a special case of 42 answer which comes before question.
Michael Krelin - hacker
thanks, the 'question' trick should work great for me
mrinject
A: 

It goes like this:

myVariable = this.testMethod() ? 'value for true case' : 'value for false case'
A: 

in python I read it as a normal English sentence:

 a equals b if condition is true else c
SilentGhost
am I correct in thinking that down voters are just jealous because their language of choice is so retarded?
SilentGhost
Perhaps they are (I'm not one of them by the way), but your answer also isn't particularly helpful to this question. No one has trouble remembering the order of the keywords in the Python equivalent. That is one of the great things about Python. The BDFL and community take such pains to make the language as pleasant, readable, and consistent as reasonably possible. But knowing it doesn't necessarily help you with C-like languages.
A. Levy
no
Michael Krelin - hacker
@A. Levy: with exactly the same straight face I could claim that no one has trouble remembering C-style syntax because it's more natural to produce "question/yes/no" rather than "question/no/yes".
SilentGhost
Having used the C style for twenty years before Python, I sometimes have trouble remembering the Python equivalent and type ?: instead.
Pete Kirkham
+6  A: 

As far as remembering which symbol comes first, I just think of the fact that the first part is a question, "Is it true or not?", so the question mark goes first.

I think of the syntax in this manner

Question ? Yes : No
Brandon Bodnár
A: 

Think of it this way: a ternary statement consists of three parts: the question, the code to execute if the answer to the question is "yes" and the code if the answer is "no". The "?" comes after the question like it does in English sentences.

sepp2k
A: 

"?" is a question mark so it means "if".

A colon means, "now it comes", "then do".

The good thing about the ternary operator is that you are not forced to use it, especially if you are having problems remembering the syntax. Just use an if-statement which is more readable most times.

And no - the ternary has no better performace then an if-statement.

codymanix
Why -1? The OP asked to know how he can remember the syntax, that's exactly what I tried to explain. Was anything wrong I was saying?
codymanix
A: 

If you're unit tests still pass when you get it wrong, then either it doesn't matter or your tests aren't covering enough of the paths through the code. If there's too long a gap between typing it and getting a pass/fail from the tests, that's another issue. Very few little syntax nits matter in the presence of good, fast tests.

Pete Kirkham