484
got him (lemy.lol)
submitted 7 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
[-] eager_eagle@lemmy.world 6 points 7 months ago* (last edited 7 months ago)

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 the validation behind just the same.

And that conditional indents your entire function one level - if you have more validation checks, that's one level of indentation per check (or a complicated condition, depends whether you can validate it all in one place). It's pretty much the case the other user illustrated above.

Returns inside business logic past validation is where the problematic bugs of this class show up

That much we agree. But again, this is not an early return issue, putting too much logic in a function is the issue. Rewriting it without early returns won't make it much clearer. Creating other functions to handle different scenarios will.

[-] avidamoeba@lemmy.ca 3 points 7 months ago* (last edited 7 months ago)

Again, if you can write it with conditionals and returns, you can write it with equivalent number of conditionals and a single nested scope. No further scopes are needed. The conditional will even look nearly identically.

this post was submitted on 09 May 2024
484 points (100.0% liked)

Programmer Humor

32731 readers
451 users here now

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

Rules:

founded 5 years ago
MODERATORS