• sugar_in_your_tea
    link
    fedilink
    arrow-up
    1
    ·
    1 year ago

    I disagree. I’ve had to debug messes where errors are only really caught at the top level, and often there’s something that programmer could’ve done to properly handle it but didn’t because it wasn’t clear that the function they called could produce an error.

    And that’s where Go and Rust do a fantastic job imo. Since you’re forced to acknowledge errors, you give the programmer the opportunity to handle them and take corrective action. I like the Rust syntax a bit more because it’s easy to return errors without messing with logic flow, so you can handle all errors at the calling function easily if that’s better for logic flow.

    The best solution for Python, imo, is a mix. Here are some things I’d love to see that are related here:

    • optional chaining - x = y?.z ?? DEFAULT instead of x = y.z if y else DEFAULT (esp for nontrivial nesting)
    • monads - return can be an error or value, and you need to determine which before unpacking it; could work with optional chaining as well (would return from the function with the error, otherwise continue with the data), or require destructuring (e.g. with match blocks); similar to try/except, but there’s no assumption of success

    Some libraries used to do it this way, such as Marshmallow (used a monad pattern).

    Sometimes its cleaner and more robust this way, and sometimes it’s better to throw errors. There should be simple syntax to pick between them (e.g. like Rust’s .unwrap()).