Refactor Your Code #14
Chapter 2, Master the Editor
|
67
HACK
interface IStart
{
bool IsStarted { get; set; }
}
Visual Studio will also modify the existing class to implement the new inter-
face. The Extract Interface function is a great time-saver when you need to
create an interface that is based on a current class.
Parameter Functions
The next three functions on the menu all focus on working with parame-
ters. Since the sample class does not currently contain any methods with
parameters, go ahead and add a new method with a number of different
parameters. Here is a method called
Collision that you can add to this class:
public void Collision(DateTime CrashDate, int Speed, int DamagePct)
{
int costMultiplier = 1;
// Do stuff
}
As you can see, this method has a local variable called costMultiplier and
sets the value to
1. But suppose that costMultiplier varies from vehicle to
vehicle. In this case, you’d want to pass in this multiplier as a parameter
instead of creating it as a local variable. The first of the parameter refactor-
ing functions will do just that. First, right-click on the
costMultiplier vari-
able and select the Promote Local Variable to Parameter function. This will
take the local variable and add it as a parameter to the method. Here is a
look at the modified code:
public void Collision(int costMultiplier, DateTime CrashDate,
int Speed, int DamagePct)
{
// Do stuff
}
You may be thinking that this is not a big deal; it is basically cut and paste.
But Visual Studio also goes and looks for any calls to this method and modi-
fies them as well. If you were calling this method before with the following
line of code:
Collision(DateTime.Now, 60, 10);
this method call would be changed to the following:
Collision(1, DateTime.Now, 60, 10);
Notice that Visual Studio not only adds the new parameter, but also passes
in the value that the local variable was set to as well, saving time and ensur-
ing that existing code continues to work as it did before.
68
|
Chapter 2, Master the Editor
#14 Refactor Your Code
HACK
Suppose you decide that you don’t need the CrashDate parameter. If you
won’t be using it in the method, you can remove this parameter using the
second of these parameter refactoring functions, called Remove Parameters.
First, right-click on the method name and click Remove Parameters in the
Refactor menu. You’ll be shown the Remove Parameters dialog, which is
shown in Figure 2-37.
In the Remove Parameters dialog, you can select the
CrashDate parameter,
click Remove, and then click OK. You are then shown a Preview Changes
dialog showing the changes that will be made to any calls to this method.
After clicking Apply on that screen, those changes will be made. Any calls to
this method will have this parameter removed, and the parameter will be
removed from the method signature.
Further, suppose you decide that you don’t want
costMultiplier to be the
first parameter, but rather the last parameter. This is where the last of the
parameter functions comes in. First, right-click on the method, then choose
the Reorder Parameters menu item on the Refactor menu. This will display
the Reorder Parameters dialog, which is shown in Figure 2-38.
Figure 2-37. Remove Parameters dialog

Get Visual Studio Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.