Live data from Hacker News

Is This a Branch?

bartwronski.com

31–40 of 78 posts

Re: Is This a Branch?

#31

Earlier quoted context omitted.

It's important to note that ternaries are expressions, they evaluate to a value. When I see a ternary I know that a boolean is being mapped to a value. This is true both technically and almost always in practice. If-statements are the wild west. Who knows what an if statement will do? An if-statement may be as simple as a ternary, or it might be a branch point and the resulting logic streams will never meet again.

We're talking C. As soon as a ternary calls a function, all bets are off what could be happening in addition to the value being computed. And thanks to the preprocessor, any identifier could potentially be calling a function.

Hmm? Which part implies “all bets are off”?

There’s the condition (evaluated first), the true case and the false case. Only one of the cases is evaluated.

I’m assuming you were alluding to some sort of “x++ = x++ * *x++” style situation, but the ternary expression isn’t a statement and there are sequence points in between. As a bonus, the first result for “ternary operator evaluation order” gave me someone who linked to the standard [1]. Quoting from the standard that they quoted:

> The first operand is evaluated; there is a sequence point between its evaluation and the evaluation of the second or third operand (whichever is evaluated). The second operand is evaluated only if the first compares unequal to 0; the third operand is evaluated only if the first compares equal to 0; the result is the value of the second or third operand(whichever is evaluated), converted to the type described below.

[1] https://stackoverflow.com/questions/65109494/order-of-evalua...

Re: Is This a Branch?

#32
post #3

Ifs are generally bad for readability though, because they force the reader to understand your control flow. Replacing an if with a ternary makes it more readable, not because the generated code is any different, but because a reader immediately knows that they don't need to scan through the two sides for control flow constructs (because the two sides of the ternary are guaranteed to be expressions rather than blocks…

I have been reading HN for a long time but your comment is the first time I violently felt the need to create an account and respond. I want you to consider how significant that is. Ternaries are NEVER easier to read.

What if you write them like this?

  int x = (cond)
     ? 5
     : 1;

Re: Is This a Branch?

#33
post #19

Earlier quoted context omitted.

I have been reading HN for a long time but your comment is the first time I violently felt the need to create an account and respond. I want you to consider how significant that is. Ternaries are NEVER easier to read.

Imho the problem with the ternary isn't the concept but the literal ?: symbols used for it. So many languages use clear words like "if" and "then" and "else" and parens and curly braces for conditional statements... But then they go full Perl and mash their faces against the keyboard when developing syntax for conditional expressions. It's bone-headed language-design that C did wrong and everybody else continues to d…

I've been messing around with some code that uses:

  #define TERN(c, a, b) c ? a : b
I'm not a fan of ternaries, but that macro makes it kind of more ligible.

Re: Is This a Branch?

#34
post #3

Ifs are generally bad for readability though, because they force the reader to understand your control flow. Replacing an if with a ternary makes it more readable, not because the generated code is any different, but because a reader immediately knows that they don't need to scan through the two sides for control flow constructs (because the two sides of the ternary are guaranteed to be expressions rather than blocks…

I have been reading HN for a long time but your comment is the first time I violently felt the need to create an account and respond. I want you to consider how significant that is. Ternaries are NEVER easier to read.

I am surprised about this. Very often you must duplicate a lot of the statement in each branch of the if statement, and it's not clear that those duplicated parts are exactly the same and must be exactly the same.

Re: Is This a Branch?

#35
post #33
post #19

Earlier quoted context omitted.

Imho the problem with the ternary isn't the concept but the literal ?: symbols used for it. So many languages use clear words like "if" and "then" and "else" and parens and curly braces for conditional statements... But then they go full Perl and mash their faces against the keyboard when developing syntax for conditional expressions. It's bone-headed language-design that C did wrong and everybody else continues to d…

I've been messing around with some code that uses: #define TERN(c, a, b) c ? a : b I'm not a fan of ternaries, but that macro makes it kind of more ligible.

You may wish to look at the Bourne shell source for other macros to make C readable.

Re: Is This a Branch?

#36
post #3

Ifs are generally bad for readability though, because they force the reader to understand your control flow. Replacing an if with a ternary makes it more readable, not because the generated code is any different, but because a reader immediately knows that they don't need to scan through the two sides for control flow constructs (because the two sides of the ternary are guaranteed to be expressions rather than blocks…

I have been reading HN for a long time but your comment is the first time I violently felt the need to create an account and respond. I want you to consider how significant that is. Ternaries are NEVER easier to read.

Only a sith deals in absolutes.

Re: Is This a Branch?

#37
post #35
post #33

Earlier quoted context omitted.

I've been messing around with some code that uses: #define TERN(c, a, b) c ? a : b I'm not a fan of ternaries, but that macro makes it kind of more ligible.

You may wish to look at the Bourne shell source for other macros to make C readable.

https://news.ycombinator.com/item?id=22191790

Re: Is This a Branch?

#38
post #3

Ifs are generally bad for readability though, because they force the reader to understand your control flow. Replacing an if with a ternary makes it more readable, not because the generated code is any different, but because a reader immediately knows that they don't need to scan through the two sides for control flow constructs (because the two sides of the ternary are guaranteed to be expressions rather than blocks…

Early return ifs are great.

I usually just transform functions with 2+ if statements into functions without `if` statements by extracting `if` statements into other smaller functions where I can write it with an early return.

Looks great.

Re: Is This a Branch?

#39

Earlier quoted context omitted.

I have been reading HN for a long time but your comment is the first time I violently felt the need to create an account and respond. I want you to consider how significant that is. Ternaries are NEVER easier to read.

What if you write them like this? int x = (cond) ? 5 : 1;

I personally prefer

    int x = (cond) ?
        5 :
        1;
Since the question mark applies to the condition and not the 5. This also highlights the 2 possiblities of the value in a more clear way. The two possible values are directly stacked with nothing in front of them.

Re: Is This a Branch?

#40

Earlier quoted context omitted.

Yes, the : and ? are single characters that are easily lost/overlooked among the rest of the code on that line. Even worse when there are nested ternaries.

When I find a place where a complex ternary is probably the best choice, I'm a big fan of the following format, because I find it clear that each line is a new condition myvar = condition_1 ? value_1 : condition_2 ? value_2 : condition_3 ? value_3 : value_fallback;

> where a complex ternary is probably the best choice

Under what circumstances would that happen? I've never encountered a nested ternary that wouldn't be better replaced with readable code, so now I'm wondering what experiences I've been missing. :)

Post reply on HN