Search code examples
coding-stylecode-readability

Function with multiple exit-points: is it a good way to do?


during programming with VBScript I write a lot of error-check code in functions before function start to do actions. So, if some pre-reqierements wan't met, then I do "Exit Function". So, for example:

public fucnton func
   if not condition then
     func = -1
     exit function
   End If
   'Other conditions with exit functions
   'Then long code goes here
   ..........
   ..........
   ..........
   func = res
End Function

So, I can exit from function in multiple points. Is this good way to do? In this case I will get long else branches of if statement

Maybe it is better to write:

public fucnton func
    if not condition then
        func = -1
    Else
        'Then long code goes here
        ..........
        ..........
        ..........
    End If  
End Function

Please, share your thoughts.


Solution

  • I've used 'if (badParameters) then exit' style of coding for decades. If nothing else, the actual 'long code' that does the work is not pushed off the right-hand side of the edit window by a massive 'if/then/else' ladder. The ladder makes the code look messier and more complex than it actually is and impedes readability.