KnowDotNet

Refactor Methodology - Stub New Method

Refactor While You Code

by Les Smith

Would you like to be able to stub out a new method while you are creating the call to it in the current method?  This is a common task in day to day development and there is an automated solution.

This is another article on a continuing series that I have been writing on Refactoring.  As I have said, so many times before, Refactoring is a Mind Set.  If you code with refactoring principles in mind, you will not have to spend huge amounts of time going back and cleaning up your code.  Keep your code clean, modular, readable, and commented as you go, and you will spend less time debugging, and everyone will be happier with your code when they have to make a change or enhancement to it.

Some developers, who are probably more organized than I am, like to stub out all of their methods in a class before they begin to code any of them.  I tend to start coding a driver method, that will call other helper methods to get the job done in a class.  If you happen to follow that methodology of coding, or even if you take the former tack, you will inevitibly find that you need to call a method that has not been stubbed out.  Therefore, when you code the calling line, the method is undefined.  When you are coding a call to a non-existant method, you would like to be able to Stub the New Method, without having to type it and place it yourself.  Net Refactor, KnowDotNets popular refactoring add-in, now supports this feature, for both C# and VB.NET developers.

To use the Stub New Method feature of Net Refactor, code your call to the non-existant method as shown in Figure 1.  

Figure 1 - Calling the New Method.

Call New Method


Once the calling line has been coded,
select just the name of the new method as shown in Figure 1.   Next, right-click on the selection and choose the Stub New Method menu item of the Net Refactor context menu.  Net Refactor will analyze the calling line in the context of the current method in which it resides.  It will construct the new method, either in VB.NET or C# and display the code in the Stub New Method dialog as shown in Figure2.

Figure 2 - Stub New Method Dialog.

Stub New Method Dialog


If Net Refactor cannot determine the type of the parameters or the return value, where applicable, the variables will be typed as Object and comment denoting this fact will be placed before the method.  If you want to make changes to the method, or even place some code in the new method, make the changes in the code window of the dialog before clicking the Save Button.

When you click the Save button, the new method will be saved after the end of the current method.