• Avid Amoeba
    link
    fedilink
    4
    edit-2
    1 year ago

    I’m sure you are capable of rewriting that in 3 lines and a single nested scope followed by a single return. In fact in languages that use exceptions you have to use at least one subscope.

    Notice that in my example I didn’t even broach the example with error conditions, cause it’s trivial to write cleanly either way. Instead I talked about returns inside business logic. You can’t unfuck that. 🐞

      • @Miaou@jlai.lu
        link
        fedilink
        -11 year ago

        goto is used in C for this exact kind of early return management. The person you answered to does not maintain code I think

        • Avid Amoeba
          link
          fedilink
          51 year ago

          goto cleanup is not the same as return. I didn’t badmouth goto cleanup.

          • @Miaou@jlai.lu
            link
            fedilink
            11 year ago

            This is virtually the same thing with a different keyword, I’d like to hear where you (and the down voters) draw the line.

      • Avid Amoeba
        link
        fedilink
        4
        edit-2
        1 year ago

        Not sure why you had to do the inverted predicate check again in your first example. You already have the information encoded in the value of retval. It can be written like this:

        int result = 0;
        if (!p1) result = -ERROR1;
        if (p2) result = -ERROR2;
        if (!p3 && p4) result = -ERROR3;
        if (result != 0) {
            result = 42;
        }
        
        return result;
        

        With a return value you have to add 4 extra lines. This overhead remains constant as you add more checks and more business logic.

        Yes all the other suggestions are better than early returns in business logic and would help with leaks. Would be nice if we had RAII outside of C++. I think Rust has it? Haven’t done Rust yet.