• 2 Posts
  • 212 Comments
Joined 1 year ago
cake
Cake day: July 5th, 2023

help-circle


  • Avid Amoeba@lemmy.catoProgrammer Humor@lemmy.mlMeetings vs productivity
    link
    fedilink
    arrow-up
    11
    arrow-down
    2
    ·
    edit-2
    9 days ago

    I’m serious. Politics are a good chunk of the job, meetings is a major place for that. What happens there can have dramatic effects on how long something takes and therefore on the “produced output per unit of time.” I’ve been at it for 13 years now and embracing that has had positive results on my well-being and career. 🥹







  • Have you stopped to consider why you can’t explain it better? Perhaps the reason is because you’re wrong.

    Yes I have. You’ve already assumed I’m not too bright more than once and worked from there. There’s no point in investing more work on my end. If what I said worked, good. If not, that’s fine too.



  • Avid Amoeba@lemmy.catoProgrammer Humor@lemmy.mlgot him
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    2 months ago

    Yes clearly someone has to read the blocks at least once to ensure they are correct.

    In subsequent reads, when I’m interested in the second block out of two, say during a defect analysis, I don’t have to read the first one to be sure I’m going to reach the second. I can straight head for the second one and any subsequent stuff I care about. Multiple returns force me to read both blocks. I don’t know what else to tell you. To me this is obvious and I think it’s probably even provable. I don’t know about you but I have to read a lot of existing code and every bit helps. We have pretty strict code style guides for that reason.



  • Avid Amoeba@lemmy.catoProgrammer Humor@lemmy.mlgot him
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    2 months 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.




  • Avid Amoeba@lemmy.catoProgrammer Humor@lemmy.mlgot him
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    2 months ago

    I don’t think it’s worse, I think it’s equivalent. Also I don’t like the risk of resource leaks which is inherent to multi-returns beyond input validation. And that’s true beyond C because memory isn’t the only resource that can be leaked.

    It’s not about how readable the branches are, it’s about having to read all of them to ensure you understand the control flow so that you don’t leak. Length of functions is a red herring. You want me to read the contents of short blocks to ensure the control flow is correct. I don’t want to read the contents of those blocks, other than the conditional and loop statements. Reading short blocks is better than reading long blocks. Reading just the control flow lines is better than reading short blocks.



  • Avid Amoeba@lemmy.catoProgrammer Humor@lemmy.mlgot him
    link
    fedilink
    arrow-up
    3
    arrow-down
    3
    ·
    edit-2
    2 months ago

    I never said longer functions are not less clear. I said my argument is valid irrespective of the length of the function which shows that the problems I claim multiple returns bring are independent of function length. 😊

    Any validation you can write with a few early returns you can write with an equivalent conditional/s followed by a single nested block under it, followed by a single return. The reader is free to leave if the validation fails nearly the same, they have to glance that the scope ends at the end of the function. Looks at conditional - that’s validation, looks at the nested block - everything here runs only after validation, looks after the block - a return. As I mentioned in another comment, validation is a trivial case to do either way. Returns inside business logic past validation is where the problematic bugs of this class show up which requires more thorough reading to avoid.

    If you gave me a PR with early returns only during validation, I probably won’t ask you to rewrite it. If I see them further down, it’s not going in.


  • Avid Amoeba@lemmy.catoProgrammer Humor@lemmy.mlgot him
    link
    fedilink
    arrow-up
    5
    arrow-down
    1
    ·
    edit-2
    2 months 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. 🐞


  • Avid Amoeba@lemmy.catoProgrammer Humor@lemmy.mlgot him
    link
    fedilink
    arrow-up
    4
    arrow-down
    6
    ·
    edit-2
    2 months ago

    Early returns are very similar to gotos. One level of nesting to take care of validation is trivial in comparison. You’re replacing logical clarity for minimal visual clarity. This is true regardless of the size of the function which shows that the size of the function isn’t the determinant. You’re not alone in your opinion, clearly, and I’m not going to convince you it’s a bad practice but I’ll just say what I think about it. 😅 This practice doesn’t make it my team’s codebase.