spaLOGICng
Member
- Local time
- Today, 12:59
- Joined
- Jul 27, 2012
- Messages
- 164
I would use a Boolean variable to skip but stay inside the loop and what I do inside the loop, or use Exit For to escape the Loop.My old functions were serious pyramid code, often multiple nested if's deep, since then I've been using guard clauses and setting values/checking values though a series of separate if's instead of nesting, it's definitely easier to keep track of the "end" compared to nesting!
However I'm stuck when it comes to loops as there's no "continue" function to "exit sub" from that loop based on the guard clause, I suppose I could call the function repeatedly instead of it being a loop, but that wouldn't work for a nested loop that's relying on values from the main function, I know they could be passed but that's back to getting harder to read and jumping around more than "goto SkipLoop" would be?
I'm not bothered either way about goto or not goto, but the convention is there for a reason and I'm curious to have other opinions on best practice
I would use GoTo's or GoSub ... Return if I need to handle many sub routines inside the same sub procedure or function. If those sub routines can be used elsewhere, I will just create function to handle it. I would prefer a separate function before a Gosub ... Return.
Tip: Before I ever write any VBA I try to determine how much of what I am about to write can be accomplished in a query or series of queries. Why? Because if the DB will ever be migrated to SQL Server, it is a lot easier to migrate queries to VIEWS than it is VBA to Stored Procedures.