Step 1 - Select Code to Extract

Top  Previous  Next

Consider this code:

 

Do While (i > 0) And (InStr(delimiters, Mid$(sourceStr, i, 1)) > 0)

    i = i - 1

Loop

 

It seems to decrement i until the ith character in sourceStr is a delimiter.

 

OK, now we've figured out what it is. Should we insert a comment to document that? It is one of the principles of refactoring to prefer method names to comments.

 

If this is a new concept to you, you're probably thinking, "What?". Follow along with this example, and it should become clearer (for a good explanation, I refer you again to Fowler's Refactoring).

 


 

So, to begin with, highlight the routine:

 

lastWord2

 

and click on the Extract Method button on the CodeShine Toolbar.

 

If you're new to refactoring and OO design - or even if you're not - you may be thinking that it's ridiculous to extract three lines of code with no obvious reusability potential to it's own routine. According to (my interpretation of) Fowler, the presence of really short, very well-named methods is a key indicator of a maintainable program design. Follow along with the example, and maybe you'll agree.