|
The First Example - A Short Continuation |
Top Previous Next |
|
Exit Function and Exit Sub Let's take a look at our refactored source and see if there's anything else we want to do to it. We see our new call to positionOfLastDelimiter in the middle of it. Let's think about the code block right after that - it exits the function if there were no delimiters found in the string.
In an ideal world, I'd replace the selected code clause with a 1-word method call like exitIfNoDelimiters. However, since it's not really possible to exit a calling Sub from the called Sub, that won't work. That doesn't mean the CodeShine won't let you do it, but it will warn you about this...
CodeShine Warnings Let's see what happens if we select this routine and click Extract Method:
A warning is clearly shown - CodeShine will let you extract the code, but it will add extra code in order to preserve the functionality. In fact, it adds another variable called cancelled to the routine, so that the extracted code looks like this:
Private Sub exitIfNoDelimiters(sourceStr As String, i As Integer, ByRef cancelled As Boolean) If i = 0 Then 'there are no delimiters in the source string lastWord = sourceStr cancelled = True Exit Function End If End Sub
And the call to the code looks like this:
Dim cancelled As Boolean cancelled = False exitIfNoDelimiters sourceStr, i, cancelled If cancelled Then Exit Sub
Now, in this short example, we haven't really gained anything, because we've extracted 4 lines of code, but we've had to add 3 lines to the source routine to handle the exiting issue. However, in a larger example this could be a big help.
|