Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21116

[RESOLVED] Using Exit Sub within loops and if - Good or Bad?

$
0
0
Been looking at some example code and I'm seeing a lot of Exit Sub" used to prematurely exit a loop or if statements. Is there any issues with this or just bad practise?

I tend to avoid it (not sure why), but finding it easier with more complex loops and if statements.

Example

Code:

found =false
do until found or eof
 if recordfound then
    found = true
  else
    nextrecord
loop

vs

Code:

do until eof
  if recordfound then
    exitsub
  else
    next record
loop

Assuming both set of sub's have no additional commands.

Code:

if eof then
  do some stuff
else
  next record
  do 20 more commands
end if

vs

Code:

if eof then
  do some stuff
  exit sub
end if

next record
do 20 more commands

The only reason I would use the exit sub in the "IF" scenario is so I don't have 20 more commands indented and nested within the if statement. As when I have complex if-else or case statements, I can exit sub each time when I know I've hit all the validations I need.

Viewing all articles
Browse latest Browse all 21116


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>