Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Refactoring in Visual C# 2005
By Joe Mayo

Rate This Article: Add This Article To:

Refactoring in Visual C# 2005 - ' The Generate Method '
( Page 4 of 5 )

Generate Method

The practice of test-first development has gained popularity because it makes the developer think more deeply about the code they will write.

To employ this technique, you simply write your test and then you write your code. In Visual C#, this process is made easier by the Generate Method refactoring.

Once you write your test, the method call appears in the editor with a smart tag. Hover over this smart tag to view the drop-down list, click the down arrow, select Generate method stub for <method name> in <type name> as shown in Figure 6.

This creates the method in the type specified by the instance reference, as shown in Listing 6. The default implementation throws an exception, which you can then replace with your code.

Promote Local Variable to Parameter

Suppose you have a local variable set to a constant value and notice that another method at the same level uses the same value or you are creating a new method at the same level that will need that value. You can remove the redundancy by defining the variable at the calling method level and then pass the value as a parameter. Then if the value changes in the future, it will be updated for each method and reduce the possibility of a subtle bug in your program. The Promote Local Variable to Parameter refactoring would help in such a situation.

Another reason to use the Promote Local Variable to Parameter method is if responsibility for managing the value belonged to a separate class, which the calling method was a member of.

Given the code in Listing 7, right-click the message parameter in ShowMessage, select Refactor, and select Promote Local Variable to Parameter. This will transform the code to what you see in Listing 8.

Listing 7. This is a typical method with a local variable. Assuming you want to manage the value of that local variable at a higher level, you can do a Promote Local Variable to Parameter refactoring.

        public void RespondToUser()
        {
            ShowMessage();
        }

        private void ShowMessage()
        {
            string message = "Success!";

            Console.WriteLine(message);
        }

Listing 8. Implementing a Promote Local Variable to Parameter refactoring removes the local variable from the method, creates a new parameter of the same type, and modifies the caller so it passes the value.

        public void RespondToUser()
        {
            ShowMessage("Success!");
        }

        private void ShowMessage(string message)
        {
            Console.WriteLine(message);
        }

One thing you'll notice in the Promote Local Variable to Parameter refactoring is that the local variable is deleted, a new parameter with the same name and type is created, and the calling method is passing the value. Assuming that you needed to use this value in multiple methods, you would need to create a local variable in the calling method to hold the value, which could be passed to multiple methods.

Reorder Parameters

When coding a method, you know what parameters are necessary, but may not immediately think about their order. Often, it doesn't matter, but on occasion you notice that the implementation could have been more intuitive with a better ordering. The Reorder Parameters refactoring allows you to reorder parameters at both where a method is defined and all callers.

To perform a Reorder Parameters refactoring, right-click on one of the parameters, select Refactor, and then select Reorder Parameters. Given the code in Listing 9, you'll see a Reorder Parameters dialog similar to the one shown in Figure 7.

Listing 9. Sometimes the order of parameters is not as intuitive as you would like. This particular listing has a key after the object it represents and could make more sense the other way around.

public void InsertCustomer(Customer cust, int custID) { }




Discuss Refactoring in Visual C# 2005
 
>>> Be the FIRST to comment on this article!
 


 
 
>>> More Using Microsoft Visual Studio Articles          >>> More By Joe Mayo