• Ephera@lemmy.ml
    link
    fedilink
    English
    arrow-up
    5
    ·
    1 day ago

    I prefer if-expressions where possible. For example, this is valid Rust:

    let x = if is_y {
        y
    } else {
        z
    };
    

    (Can also be on a single line.)

    This is the same syntax as the normal if-statement, except the compiler forces you to add an else-branch, if you want to ‘return’ a value from it.

    Don’t tell anyone, but the ternary operator is when the C designers realized that being purely procedural is cumbersome AF. 🙃
    Unfortunately, they decided that expressions need to look like math, so now JS devs get to write random question marks and colons across many, deeply nested lines of code.