8

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.

Matthew Rapati
  • 5,648
  • 4
  • 28
  • 48
  • 5
    never thought it's possible to get this backward… – Michael Krelin - hacker Sep 15 '09 at 18:01
  • 2
    operatorName = isPedantic() ? "conditional" : "ternary"; – Adam Bellaire Sep 15 '09 at 18:01
  • 5
    Why the hate on this question? It's a bit light, but I mean, it's not like "Jon Skeet Facts" or something. – Chuck Sep 15 '09 at 18:05
  • Seriously, I always have to check this, I don't know why. It always slows me down while I'm programming. – Matthew Rapati Sep 15 '09 at 18:07
  • divo, you repeat Adam's comment ;-) – Michael Krelin - hacker Sep 15 '09 at 18:10
  • 3
    Why the downvotes? So it is a newbie-ish question...what is the big deal? The guy needs a mnemonic to get over a mental block. If he gets a good answer to his question, he will have a better understanding of the syntax and semantics of his language of choice. That sounds like a pretty reasonable programming question to me. – A. Levy Sep 15 '09 at 18:11
  • 4
    Also, I don't think this needs to be community-wiki. Community wiki should be for polls, jokes, and career building advice. This is about actual code. Maybe there needs to be a "degree of difficulty" rating for questions to distinguish the trivial questions from the technically challenging. But that is a quantitative distinction, not a qualitative distinction like community-wiki. – A. Levy Sep 15 '09 at 18:19
  • @A. Levy: community wiki is generally for question that possibly cannot have a single correct answer. OP's soliciting personal experience of the users, how is it not subjective? what next? are we going to find mnemonics for what sigils to use for which data type in Perl? – SilentGhost Sep 15 '09 at 18:33
  • Community Wiki is for questions that you want to give ownership of to the community. – Chuck Sep 16 '09 at 02:21

8 Answers8

23

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
  • 29,056
  • 6
  • 39
  • 56
10

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 Bodnar
  • 8,202
  • 2
  • 36
  • 42
  • Some people criticize the ternary operators for not being very readable. In my opinion the higher readability depends entirely on being able to understand the meaning of the symbols. Readability may be poor for the novice, but it may also improve readability for the veteran, by making simpler logic easier to be a block, and by making simple statements take up less space from the screen. – Andy Apr 14 '21 at 00:56
0

It goes like this:

myVariable = this.testMethod() ? 'value for true case' : 'value for false case'
sth
  • 222,467
  • 53
  • 283
  • 367
0

in python I read it as a normal English sentence:

 a equals b if condition is true else c
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • 3
    am I correct in thinking that down voters are just jealous because their language of choice is so retarded? – SilentGhost Sep 15 '09 at 18:04
  • 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 Sep 15 '09 at 18:08
  • @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 Sep 15 '09 at 18:12
  • Having used the C style for twenty years before Python, I sometimes have trouble remembering the Python equivalent and type ?: instead. – Pete Kirkham Sep 15 '09 at 18:29
0

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
  • 363,768
  • 54
  • 674
  • 675
0

"?" 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
  • 28,510
  • 21
  • 92
  • 151
  • 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 Sep 15 '09 at 23:48
0

The syntax of ternary operators is <condition> ? <if-true> : <if-false>

? means if.

But if you put ? in front of an expression like if, the syntax becomes ? <condition> <if-true> : <if-false>.

<condition> <if-true> is hard to distinguish, so you have to add other symbols to split it, which defeats the purpose of the ternary operator to simplify the syntax.

So we have to put ? comes after it to solve this problem, so it is an inverted setting, which can be seen as (<condition> ?) <if-true> : <if-false>.

which reads: If <condition> then <if-true>, otherwise <if-false>.

Andy
  • 1,077
  • 1
  • 8
  • 20
-1

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
  • 48,893
  • 5
  • 92
  • 171