Hacker News new | ask | show | jobs
by AlienRobot 42 days ago
This is one of the things I like about Rust. You can have both.

    let foo = {
        if bar {
            1
        } else if baz {
            2
        } else {
            3
        }
    };
Unfortunately not all languages are Rust.
1 comments

That isn't more concise than

   foo = bar ? 1
             : baz ? 2
                   : 3;
But it's more readable, and that's what matters.
I don't think more empty lines and useless whitespace is "more readable" at all. Once again, the "taste" argument comes back.
What makes it more readable is:

1. Not having two sets of syntax for the same idea, especially when if statements can trivially be extended to an expression.

2. Not adding the concept of an operator pair (? and :) to the grammar of the language, along with parsing rules, just for this one case

3. Not getting the precedence of this ?: operator pair completely wrong.

4. The conditions are at the same indentation level, each condition is preceded by the word "if" - it's obvious what it is when skimming the code.

5. The values too are at the same indentation level, making it obvious they play the same role in the code - rather than your snippet where the condition "baz" is directly under the value 1