News for hackers. Stuff that matters.
Re:Porting 4.5K lines of C to Go by userbinator on Saturday August 05, 2017 @12:46AM (#14934274)
I don't get what value it really supplies, and it seems like people are really fond of using it in the wrong places.

It's a conditional expression , not a statement, and particularly helps simplify things like

    if(foo)
        someVeryVeryLongExpressionEvaluatingToAnLvalue = someShortRvalue;
    else
        someVeryVeryLongExpressionEvaluatingToAnLvalue = otherShortRvalue
into

    someVeryVeryLongExpressionEvaluatingToAnLvalue =
        foo ? someShortRvalue : otherShortRvalue;
I suppose to really "get" what the ternary is useful for, you have to have some experience with a more functional programming language, focusing on the flow of the data itself. There, multiply-nested ternaries are really the norm for what you'd otherwise call "control flow" in non-functional languages, and structures like this are common:

   value = isAtrue ? A_value :
           isBtrue ? B_value :
           isCtrue ? C_value :
                     D_value ;
(Does not work in PHP, but every other language with a ternary will interpret that as a compact alternative to the far more cumbersome if/else --- especially if coding standards dictate braces on their own line.)
Re:Porting 4.5K lines of C to Go by skocznymroczny on Saturday August 05, 2017 @01:19AM (#14934353)
I don't like the ternary operator ?, because it's not obvious:

xyz = foo ? bar : baz;

What does it mean? Which is the condition? which is the true, which is the false value? It's not obvious.

In Python it is much more clearer:

xyz = bar if foo else baz

Re:Porting 4.5K lines of C to Go by bshimmin on Saturday August 05, 2017 @01:47AM (#14934396)
I honestly struggle to understand how a traditional ternary like this isn't obvious. The bit before the question mark is the condition (er, like a question, which ends with a question mark), then the other two bits are for true and false in the order you would expect.

I will freely admit I try and avoid nested ternaries, which I personally find hard to read.

Re:Porting 4.5K lines of C to Go by evincarofautumn on Saturday August 05, 2017 @02:17AM (#14934453)
I never really understood the distaste for it, or how it’s non-obvious. It seems like the people I’ve talked to object to the punctuation-based syntax more than the semantics, because it exactly mirrors conditional statements, even when nested. (Well, except in PHP, but that’s a bug in the grammar.)

    if (a) { b } else   if (c) { d } else { e }
        a  ? b   :          c  ? d   :      e

    if (a) { b } else { if (c) { d } else { e } }
        a  ? b   :    (     c  ? d   :      e   )
The question mark goes after the thing you’re asking about, like in English. The colon to separate the true and false branches is totally arbitrary, I grant you. I’d prefer to have the if…else notation available as an expression, but the statement-expression distinction hasn’t quite died yet.

Personally, I don’t care for Python’s version with the condition and true branch swapped, because I read it like invalid Forth, expecting “condition IF true-branch ELSE false-branch THEN”. :P