[ACCEPTED]-Returning early from a function in classic ASP-asp-classic
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 1 a value from a function.
Function usefulFunc(str) ''# Validate Input If str = "" Then usefulFunc = "" Exit Function End If ''# Real function ''# ... End Function
With classic ASP, you need to use
Function MyFunc(str) if (str = "ReturnNow!") then Response.Write("What up!") Exit Function end if Response.Write("Made it to the end") End Function
As has been pointed out you can use
Exit Function but you 21 should use caution. In the simple example 20 you gave there really is no advantage no 19 other code would have executed
Placing 18 exit points throughout a chunk of code can 17 make it hard to follow and debug. More 16 seriously it can lead subsequent changes 15 to the code being harder, requiring more 14 extensive changes and therefore increasing 13 the risk. Hence such a pattern should be 12 considered a "bad smell".
A typical scenario 11 where its reasonably acceptable is where 10 the code may make some assertions on its 9 input parameters before continuing with 8 the body of the code. Other than that 7 you should be able to express a really, really 6 good reason to do it.
You may say "If I do 5 it that way I'll have more
If structures and 4 increase the identation in the code excessively". If 3 that is so then the function has too much 2 code in it anyway and should be refactored 1 into smaller functions.
More Related questions