News for hackers. Stuff that matters.
Re:Clear is better than clever [pdf] by jimbokun on Monday May 06, 2019 @11:16AM (#19840178)
"Personally, I find that ternary example you posted to be terrible code."

It's not about the line count, it's about expressing the main idea clearly and succinctly, which this code does.

My nitpick would be with the language constructs themselves. The ternary operator is simply a crutch for lack of an if-then expression returning a value.

    if a < b then -1 elsif a > b then 1 else 0
Or can add some white space to make the structure a little more clear:

    if a < b then
      -1
    elsif a > b then
      1
    else
      0
    end
Expressions like the ternary operator micro-optimize by replacing tokens recognizable as natural language words with arbitrary punctuation. But that's a language level complaint, which you generally don't have any control over once you've started the project or when editing existing code.
Re:Clear is better than clever [pdf] by userbinator on Monday May 06, 2019 @08:16PM (#19845240)
by replacing tokens recognizable as natural language words with arbitrary punctuation

It's far from "arbitrary" --- and I suspect that this dogmatic, misguided way of (not) thinking about it is responsible for the majority of the complaints and aversion, since as others have mentioned, it is completely equivalent in structure to if/else!

What do I mean by "far from arbitrary"? Well... what is the character used in basically all Latin (and even some widely-used non-Latin) languages to denote a question? I've already used that character twice in this post so far, and you should've found the previous two sentences to be pretty clear, so stop pretending the ternary operator is something scary and "unreadable" and see it for what it is: it is literally asking a question!

    understand ? done() : keep_thinking();
Re:Clear is better than clever [pdf] by oblio on Tuesday May 07, 2019 @04:36AM (#19847546)
True, but that second part kind of falls flat when we continue your analogy.

We ask questions with ? but we don't offer alternative answer to the question with :. If anything, maybe it should be ; or even .?

Re:Clear is better than clever [pdf] by pier25 on Monday May 06, 2019 @11:23AM (#19840232)
> It's not about the line count, it's about expressing the main idea clearly and succinctly

I disagree. I find no clarity in using nested ternaries, but I understand it's a matter of opinion.

Re:Clear is better than clever [pdf] by michaelcampbell on Monday May 06, 2019 @01:12PM (#19841504)
I find German inscrutable, and yet Germans don't. Everything's hard to read until you learn to read it.
Re:Clear is better than clever [pdf] by rishav_sharan on Tuesday May 07, 2019 @07:21AM (#19848384)
Or don't learn German but continue speaking in English which is inherently same as German in functionality but at least you and people around you can understand each other.
Re:Clear is better than clever [pdf] by uryga on Monday May 06, 2019 @11:31AM (#19840321)
it's also nice with Haskell's Multiway If:

  x = if | a < b     -> -1
         | a > b     ->  1
         | otherwise ->  0
 
(and `cond` in lisps)
Re:Clear is better than clever [pdf] by rishav_sharan on Tuesday May 07, 2019 @07:20AM (#19848378)
> It's not about the line count, it's about expressing the main idea clearly and succinctly, which this code does.

Thats very subjective. As a beginner dev, the ternary series is almost un-understandable for me. While the if/else blocks are far easier to glance at and digest.

Re:Clear is better than clever [pdf] by c22 on Monday May 06, 2019 @02:11PM (#19842125)
In Python you can write it like this:

  if      a < b: x = -1
  else if a > b: x =  1
  else:          x =  0
Which a lot of people seem to hate for some reason, but I always appreciated the compromise between terseness and clarity.
Re:Clear is better than clever [pdf] by o_pax on Tuesday May 07, 2019 @04:30AM (#19847527)
Python has its version of the ternary operator as well. It may look unusual due to the different order of the operands, but it works quite nicely, in my opinion:

  x = (-1 if a < b else
       1  if a > b else
       0)
This emphasizes the possible values that x may be assigned to.
Re:Clear is better than clever [pdf] by c22 on Tuesday May 07, 2019 @03:46PM (#19852801)
I agree, the ternary style is better for this sort of variable assignment situation. Usually if I am using the above pattern I am calling different functions as the result of the conditionals.
Re:Clear is better than clever [pdf] by Technetium_Hat on Wednesday May 08, 2019 @10:47AM (#19859290)
Python uses the `elif` keyword if you want to place an `else if` on one line. And column indentation isn't very "pythonic", apparently.