News for hackers. Stuff that matters.
Re:Clear is better than clever [pdf] by kazinator on Monday May 06, 2019 @01:49PM (#19841917)
I don't agree. It occludes the nested structure.

We can regard the condition as the head of a clause, and the ? and : as heads of sub-clauses:

   A ? B
     : C
which leads to:

   return a < b ? -1
                : a > b ? 1
                        : 0;
Re:Clear is better than clever [pdf] by atq2119 on Monday May 06, 2019 @02:04PM (#19842054)
What you're writing here is equivalent to insisting that people do:

  if (a < b) {
    return -1;
  } else {
    if (a > b) {
      return 1;
    } else {
      return 0;
    }
  }
Re:Clear is better than clever [pdf] by kazinator on Monday May 06, 2019 @03:15PM (#19842733)
Nope, since I haven't added any non-whitespace characters, rather it is like insisting that people do:

  if (a < b) {
    return -1;
  } else
    if (a > b) {
      return 1;
    } else {
      return 0;
    }
Unlike your perfectly formatted code there, this has a bit of a visual problem: a one-liner consequent is fully braced, whereas a multi-line alternative isn't.

Ternary operators are different. They have confusing nesting and do not support the equivalent of the "if/else ladder" pattern very well.

Re:Clear is better than clever [pdf] by naasking on Monday May 06, 2019 @06:31PM (#19844603)
> It occludes the nested structure.

The nesting is irrelevant to understanding the semantics, so this objection doesn't fly. The idiom is basically just a truth table with conditions on the left and the matching value on the right. You read it left-to-right, top-to-bottom, just like all other code. The first condition on the left that matches returns the value on right-hand side.

Re:Clear is better than clever [pdf] by kazinator on Tuesday May 07, 2019 @12:09AM (#19846476)
> The nesting is irrelevant to understanding the semantics

The nesting is absolutely relevant to producing the semantics.

The following uses deceptive whitespace to suggest a nesting that is contrary to the actual nesting, interfering with understanding:

  if (foo)
    if (bar)
      xyzzy();
  else
    flop();
The ternary operator A ? B : C is the goofy invention of demented mind. In nested situations, it is mind-bendingly unreadable. It behooves us to style it in a way that reveals the abstract syntax tree structure.

In C, I would in fact recommend:

  #define if3(a, b, c) ((a) ? (c) : (c))

  return if3(a < b, -1, if3(a > b, 1, 0));
Now we can have it in one line, yet it's clear.
Re:Clear is better than clever [pdf] by naasking on Tuesday May 07, 2019 @08:16AM (#19848700)
> The following uses deceptive whitespace to suggest a nesting that is contrary to the actual nesting, interfering with understanding

Fortunately, the ternary pattern doesn't exhibit this problem. So again, this objection doesn't fly.