tags:

views:

281

answers:

3

Is there a way to return early from a function in classic ASP rather than it run the full length of the function? For example lets say I have the function...

Function MyFunc(str)
  if (str = "ReturnNow!") then
    Response.Write("What up!")       
  else
    Response.Write("Made it to the end")     
  end if
End Function

Can I write it like so...

Function MyFunc(str)
  if (str = "ReturnNow!") then
    Response.Write("What up!")       
    return
  end if

  Response.Write("Made it to the end")     
End Function

Note the return statement which of course I can't do in classic ASP. Is there a way to break code execution where that return statement sits?

+6  A: 

Yes using exit function.

Function MyFunc(str)
  if str = "ReturnNow!" then
    Response.Write("What up!")       
    Exit Function
  end if

  Response.Write("Made it to the end")     
End Function

I commonly use this when returning.

Function usefulFunc(str)
   ''# Validate Input
   If str = "" Then
      usefulFunc = ""
      Exit Function
   End If 

   ''# Real function 
   ''# ...
End Function
C. Ross
@Anthony thanks for the formatting help with the comments.
C. Ross
A: 

With classic ASP, you need to use Exit Function:

Function MyFunc(str)
  if (str = "ReturnNow!") then
    Response.Write("What up!")       
    Exit Function
  end if

  Response.Write("Made it to the end")     
End Function
Oded
+3  A: 

As has been pointed out you can use Exit Function but you should use caution. In the simple example you gave there really is no advantage no other code would have executed anyway.

Placing exit points throughout a chunk of code can make it hard to follow and debug. More seriously it can lead subsequent changes to the code being harder, requiring more extensive changes and therefore increasing the risk. Hence such a pattern should be considered a "bad smell".

A typical scenario where its reasonably acceptable is where the code may make some assertions on its input parameters before continuing with the body of the code. Other than that you should be able to express a really, really good reason to do it.

You may say "If I do it that way I'll have more If structures and increase the identation in the code excessively". If that is so then the function has too much code in it anyway and should be refactored into smaller functions.

AnthonyWJones
Some good points Anthony. It is definitely a very simple example and I used it just to find out what the proper syntax was.I was indeed going to mention your last point about having more "if" structures and increasing indentation if you don't use "exit". Personally I think this is a "religious war" issue in programming being more a matter of personal preference. Regardless your feedback is great so thank you.
Rob Segal
@Rob: I would define a "Religious" issue as a issue where choosing one side or another has little or no impact on the outcomes. Best practice on the other hand is about issues where choosing one side or another can significantly impact the final outcomes. As a general rule (bearing in mind no rules are ever absolute) it is "best practice" for any recognisable block of code to have one point of entry and one point of exit. This is true of functions, if statements, loops etc. `Exit Function` is a useful tool when a reasonable __exception__ to the "best practice" is found.
AnthonyWJones
True it certainly makes sense to have one point of entry and exit for functions and I would agree it is a good practice. My main reasoning for using "Exit Function" call would be for a point you have already mentioned which is performing various assertions and/or validations.
Rob Segal