Live data from Hacker News

Is This a Branch?

bartwronski.com

21–30 of 78 posts

Re: Is This a Branch?

#21
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…

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.

Re: Is This a Branch?

#22
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…

Sorry, perhaps I don't understand how ternary operators are any different than if; then; else. In fact for readability I very much prefer guard statements that don't include an else at all.

Re: Is This a Branch?

#23
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.

Meh. When sufficiently terse, ternary expressions are frequently more readable than a more verbose if/else equivalent. I would agree that ternaries can get out of hand quickly though.

Re: Is This a Branch?

#24
Not to mention branches and branch prediction can be a cause of understudied (and possibly) vulnerable interactions like Spectre/Meltdown.

It's also worth mentioning that branching is a critical part of any recursive function, or else it would not know when to terminate. In some cases the function call could be linearized, but it again becomes a compiler runtime cost analysis.

Re: Is This a Branch?

#25

Earlier quoted context omitted.

Maybe ternaries aren't easier to read, but they make the surrounding code easier to read because they duck out of the way? Devoting several lines to what is a very quick if/else expression could be distracting I suppose. Playing devil's advocate.

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.

Re: Is This a Branch?

#26
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…

Do you know if there's any evidence that ternaries beat ifs for people understanding code in practice or in people's self-assessments? Would not have been my guess at all.

If Hillel Wayne’s talk on empirical software design is any good measure then either method is fine[0] so long as you and the team you work with can agree about it’s utility in code review. If it’s just you on the project then do whatever is most comfortable.

[0] https://m.youtube.com/watch?v=WELBnE33dpY

Re: Is This a Branch?

#27

Lots of code has apparent branches which compilers effectively optimize. An easy example is integer signbit: int isignbit(int x) { return x which is reliably optimized to logical right shift. Are there any code patterns where the compiler emits branches that are not apparent in the code? Notwithstanding overflow checks, etc.

Some targets don't support integer division/remainder or some floating point ops in hardware and automatic vectorization can add/remove branches in places that won't be easily predictable.

Re: Is This a Branch?

#28
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.

They are clear when succint, and especially for setting a value.

-- example:

var x;

if(this)

x = 10;

else

x = 11;

-- is uglier than:

var x = this ? 10 : 11;

Re: Is This a Branch?

#29
I’d say the behavior is mostly historical.

Early GPU “compilers” [1] and their corresponding architectures really meant that “an if statement means go slow”. Also, IIRC, the original mask depth on Fermi was ... 32 branches, and afterwards you’d spill. So it was an adaptation to “don’t make the compiler work harder when you can fold the branch for it”.

I always found this to be worse for autovectorization with icc. Every once in a while, it would impress you by calculating masks and then blending. Usually it would not.

[1] Many of these “compilers” were really just blindly emitting code with no optimizations. The point was it ran correctly, and was effectively autovectorized. If you cared, you could make it faster.

Re: Is This a Branch?

#30
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…

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;
Post reply on HN