458
got him (lemy.lol)
submitted 4 months ago by ngn@lemy.lol to c/programmerhumor@lemmy.ml
you are viewing a single comment's thread
view the rest of the comments
[-] avidamoeba@lemmy.ca 4 points 4 months ago* (last edited 4 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.

this post was submitted on 09 May 2024
458 points (92.4% liked)

Programmer Humor

32192 readers
367 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS