logoalt Hacker News

3eb7988a1663today at 1:22 AM5 repliesview on HN

Never will I understand ternary operators. As soon as you introduce it, some chuckle heads want to use them everywhere. Worse if the syntax allows nested ternarys. I guess it keeps the language open for code golfing, but it otherwise seems like redundant syntax that at best saves a few characters.


Replies

demilicioustoday at 1:30 AM

That’s why “if” should just be an expression

show 2 replies
201984today at 1:26 AM

Lua basically already has ternary operators anyway since "and" and "or" short circuit. I also don't see the need of adding additional syntax for it.

  local x = condition ? value_a : value b
  local x = condition and value_a or value_b
show 1 reply
Gualdrapotoday at 1:29 AM

I guess for the JS case it makes sense to be able to shave a few characters for file shrinking purposes, but generally I'm more biased to code clarity and "self-explainability"

show 1 reply
hiccuphippotoday at 1:32 AM

I find it most useful in languages that have non-mutable variables and you want to avoid a mutable variable or an extra function when the value comes from a simple condition.

otikiktoday at 2:13 PM

In Lua (and LuaJIT) you can already use `and` and `or`:

    local x = y and y + 1 or 0
The knuckle heads are already using them everywhere.