Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

"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.


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();


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 .?


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


I find German inscrutable, and yet Germans don't. Everything's hard to read until you learn to read it.


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.


it's also nice with Haskell's Multiway If:

  x = if | a < b     -> -1
         | a > b     ->  1
         | otherwise ->  0
 
(and `cond` in lisps)


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


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.


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.


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.


Python uses the `elif` keyword if you want to place an `else if` on one line. And column indentation isn't very "pythonic", apparently.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: