And in an if-expression, you similarly can have expressions. Some of those expressions might be blocks.
Some languages restrict where block expressions can be placed, but that's not a meaningful difference between if expressions and ternaries, that's a difference how blocks are treated. More concretely: I know of no language that allows "if expressions" that aren't just ternaries (i.e. either the language allows blocks as expressions or it doesn't, but given that choice, no language then further distinguishes between if expressions and ternaries).
Just because of that they are far superior to if/else/unless, unless ;-) you're using the ternaries to execute statements with side effects instead of just returning a value.
It’s the giant nested ternaries that kill me. In nearly all circumstances I can think of (Terraform being a notable exception), ternaries could be replaced with an if/else if block and be far more readable.
In my head I’m having to cast these to an if to figure it out anyway - why not just write it like that to begin with?
You can write code withou the curly braces, but is generally considered a bad practice.
if (expression) myBlock() else otherBlock()
is really
if (expression) {myBlock();} else {otherBlock();}
In that case a block is still there and not reused. Both the semantics and structure are different if the block itself were reused. You could extend the above like so:
if (expression) {myBlock();x+=1;} else {otherBlock();}
If instead the block were reused that would not work. A finer degree of separation is imposed by reference that reenforces separated structures by name. Separation is the foundation of simplicity. It also makes for code that is easier to read like a natural language sentence.
As an aside for those wondering what the name for this is to see what other languages support it, this is a case 'if' being an expression rather than a statement which allows it to be composed within other expressions.
The if-expression and if-statement are different things.
if-statements result in conditional execution of different statements, but do not return values themselves. Borrowing C's syntax for if statements the following is not allowed:
int next = if (odd(current)) { 3*current + 1; } else {current / 2;};
That's not a valid expression in C.
if-expressions return a value, and C does have an if-expression with its ternary operator:
int next = odd(current)? 3*current + 1 : current / 2;
if-statements are ubiquitous to nearly every programming language. Even when they aren't done with the if keyword and more familiar syntax, even unstructured languages with conditional branching instructions and goto permit you to write something like an if-statement (just disguised by the usually more verbose unstructured form).
if-expressions are less universally available (though I'm unsure of any higher-level-than-assembly language still in popular use that doesn't have an if-expression equivalent).
Some languages restrict where block expressions can be placed, but that's not a meaningful difference between if expressions and ternaries, that's a difference how blocks are treated. More concretely: I know of no language that allows "if expressions" that aren't just ternaries (i.e. either the language allows blocks as expressions or it doesn't, but given that choice, no language then further distinguishes between if expressions and ternaries).
reply