Last post May 05, 2010 10:39 AM by David Anton
May 05, 2010 06:13 AM|eddy556|LINK
Using Exit Sub....is this frowned upon? If not what are the reasons it is okay? I get the feeling that it is not good practice to use them everywhere?
May 05, 2010 08:28 AM|DarrellNorton|LINK
If you are using Exit Sub or Return using a Guard Clause pattern, then it is ok. Basically, if using them makes your code EASIER to read (not harder) then I would use them.
There is a refactoring that is called Replace Nested Conditional with Guard Clause:
May 05, 2010 10:39 AM|David Anton|LINK
I agree with Darrell - in addition I'd recommend using "Return" instead of "Exit Sub".
This has the same behavior in 'Sub's, but "Return x" has subtly better behavior in functions compared to "Exit Function". Explict 'Return' in a function causes you to know on that statement exactly what you are returning, while "Exit Function" returns the
implicit function return value, which is one of those weird VB concepts that is unique to VB.