marco
cantu

Books : Mastering Borland Delphi 2005: Update for Delphi 2006

Mastering Delphi Update for Delphi 2006


Chapter 10

New Delphi 2006 Refactorings

Delphi 2006 includes a number of new refactorings, on top of those available in Delphi 2005 (and covered in the printed version of Mastering Borland Delphi 2005). Most of the new refactorings follow the formal definition of this technique: they are not merely coding helpers like a good number of those in Delphi 2005.

What is important to notice is that most of the new refactorings are part of Together support and you need to enable Together's modeling for the project to use them. The IDE will ask you to enable modeling if you use one of those refactorings and the model isn't active yet. Notice that this architecture implies that those editions of Delphi that do not include Together don't have the related refactorings.

Change Parameters

The first refactoring we look to is Change Parameters. With this tool you can change the type of an existing parameter of a method, add new parameters to the method, remove some of the existing ones, or change their orders. Of course the change is applied to the declaration and to the implementation, but when it is possible this is applied also to all of the method calls. If you change the parameters of a virtual method, changes are propagated to the override methods in derived classes. If you change a method in a base class and an existing overload method of an inherited class has the same sequence of parameters, it will become an override of the base class virtual method.

To activate this refactoring, you need to select the method name in the editor and use either the local menu or the global one. This will display the Change Parameters window:

In this dialog box you can modify the parameters (edit them, rename them, remove them, add new ones, reorder them). As you press OK you'll see a preview of the refactorings that are going to be applied, so you can figure out the effects of the changes requested before you actually perform them. Notice that in several cases, like when adding new parameters, after applying the changes the code won't compile.

Extract Interface and Superclass

The Extract Interface refactoring is one of those based on Together's modeling support. If a class has a method like:

type

  TForm5 = class(TForm)

  public

    procedure Test (x: Integer; const title: string);

  end;

by activating Extract Interface you'll be able to provide the name of the interface and pick the methods you want to extract (in this case there is only one). The final code will look like this:

type

  ITest = interface

    procedure Test (x: Integer; const title: string);

  end;

  TForm5 = class(TForm, ITest)

  public

    procedure Test (x: Integer; const title: string);

  end;

Similarly, the Extract Superclass refactoring introduces a new base class for the selected class. This refactoring is very interesting when you select multiple classes, as it will figure out the shared methods. However, it can also be used on a single class, like in the previous example. In this case the generated code will look like this:

type

  TBaseForm = class abstract (TForm)

  public

    procedure Test (x: Integer; const title: string); virtual; abstract;

  end;

  TForm5 = class(TBaseForm)

  public

    procedure Test (x: Integer; const title: string); override;

  end;

the decision whether to have the base class method like an abstract method or not depends on a check box in the configuration dialog box of the refactoring.

Introduce Field, Introduce Variable, and Inline Variable

At first sight the new Introduce Field and Introduce Variable refactorings of Delphi 2006 look very similar to the Declare Field and Declare Variable refactorings already available in Delphi 2005. Their scope, however, is quite different. In the two define refactorings you pick an undefined identifier and add a definition to it in the class or in the local scope. In the two introduce refactorings you select an expression to define a new class field or local variable that will be assigned to the expression. For example, in the code:

procedure TForm5.DisplayPosition;

begin

  ShowMessage (IntToStr (Left + Top));

end;

you can select the expression Left + Top, invoke Introduce Variable, name it position and obtain the following code:

procedure TForm5.DisplayPosition;

var

  position: Integer;

begin

  position := Left + Top;

  ShowMessage (IntToStr (position));

end;

You can also obtain the opposite effect with the Inline Variable refactoring. In this case each instance of the variable is replaced with the expression assigned to it, which is computed each time. Of course these changes can affect the actual semantic of the program, in the case where the expression evaluation causes any side effects.

The Introduce Field refactoring is similar to the Introduce Variable one, but in this case you can also set the access specifier of the field and where to add the initialization code. This is the corresponding dialog box (notice the T icon, indicating this is part of Together):

More Delphi 2006 Refactorings

There are other interesting refactorings in Delphi 2006, all based on Together support. The fact I cover them more shortly doesn't mean that they are less relevant, only that they are simpler to describe: