Use LOOPs to avoid excessive RETURNs
#5
(09-12-2022, 05:39 PM)CharlieJV Wrote: Just the way I'm wired:

When I see "EXIT DO", I immediately think there must be more that needs to be done in the subroutine, and I wind up reading further into the subroutine to see what else needs to be done.  To then find out there is nothing else to be done.  And I'm left wondering: "why the heck did you make me read all the way to the end of the subroutine.?"

When I see "RETURN", I know the subroutine is done.  I need not read the rest of the code for the subroutine, and I can get back to looking at the code that brought me to the subroutine.

Regardless, very neat tip.  I do like to see how others organise their code.  Variety is the spice of life.

Keep 'em coming!

@CharlieJV

Okay, here is another good reason to use the DO/LOOP method. Portability. Let's say we have a GOSUB routine we want to transition into a SUB or FUNCTION. With the DO/LOOP method, you just port it over. You only need to replace the GOSUB statement with a SUB or FUNCTION call, rename the label to the SUB or FUNCTION name, and of course, substitute END SUB or END FUNCTION instead of RETURN. Contrast that to the example I posted that used three inner RETURN statements. Each of those would have to be changed to EXIT SUB or EXIT FUNCTION, along with the other transition changes. More work, but with the DO/LOOP method, you only need to swap out one RETURN statement.

Pete

I'm always thinking, but that's because God put too many hot women on the planet.
Reply


Messages In This Thread
Use LOOPs to avoid excessive RETURNs - by Pete - 09-12-2022, 03:28 PM
RE: Use LOOPs to avoid excessive RETURNs - by Pete - 09-12-2022, 06:11 PM



Users browsing this thread: 2 Guest(s)