Hacker News new | ask | show | jobs
by veddan 3998 days ago
I think that the "expression-style" return looks good for short and "expression-like" function.

Good

    fn inc(a: u32) -> u32 { a + 1 }
    fn foo(a: u32, b: u32) -> u32 { let x = a + b; a * x }
Bad

    fn bar(...) -> bool {
        let mut success = false;
        let conn = getConnection();
        ...
        if x > y {
            return false;
        } else if z < q {
            success = false;
        }
        foo.barify(x, y);
        ...
        success
    }
It looks especially bad when the function has multiple early returns, and then the final return looks different.
2 comments

This could easily be added as a lint to the codebase.
Sheesh, it's such a little thing. "success" vs "return success;"
Doesn't work when you're not returning from a function (ie, EVERY OTHER PLACE a block can appear)