Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

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



sort by: page size:

That's not universally true; there are languages where if is an expression rather than a statement (e.g., Ruby.)

( ) are only optional if { } are mandatory, which is not true for some languages. e.g. in Haxe, one can write

    static function isEven(n)
        return if (n <= 1)
            n == 0;
        else
            isOdd(n - 1);

    static function isOdd(n)
        return if (n <= 1)
            n == 1;
        else
            isEven(n - 1);
i.e. An if expression consists of other expressions that may or may not be a block expression ({ }).

ternaries are expressions.

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.


An anecdotal data point: I prefer ternaries unless the if statement is for control flow. They're terser and don't stick out as much in the code.

I'm not sure about objects, but at least for control structures like "if" the answer is in your quote:

> (except for a few control structures)

Blocks are available specifically for those limited control structures, like "if".


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?


Going the other way: In languages where if is an expression

    if (a) { b; } 
would return b if a was true and nothing otherwise. Which is a similar to a && b.

It's very common for languages to adopt an everything-is-an-expression concept, and then make if/else the conditional operator.

Which is more simple, an if/else block or a terany operator?

if/else:

if i == 0 {

   return "foo"
} else {

   return "bar"
}

terany:

return i == 0 ? "foo" : "bar"


That doesn't solve the fact that blocks and conditional control flow (if/else and match) are expressions and need a way to produce values.

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.

Yes. And that is why statement–expression distinctions are stupid, because they obscure these relationships. In C-land it would be “if-else” vs. “?:”.

The if expression is not a persuasive example. C/Java/Algol/etc all have it as well.

x = something ? foo() : bar();


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


If as an expression is very nice indeed, especially in Lisp (where it comes from).

If you use CoffeeScript however, you also get expression if. Very handy.


That's a ternary, not an if expression.

I've actually never seen anyone do either of those.

I've seen

if (a) thing;

but never with an else, and certainly never with an else that has a multi-line block. That's definitely somethign I'd call out in a CR.


I presume that the alternative to a statement is an expression. For example, the ternary operator

   x = (a if c else b)
is an expression, where the conditional equivalent

   if c:
       x = a
   else:
       x = b
is a statement.
next

Legal | privacy