Search code examples
delphirefactoringdelphi-2010

Wrong behaviour/Bug in delphi2010 "extract method"


Im facing this issue when i try to extract a methode in the if statement. i couldnt find any reported bug on it.

procedure TForm1.BitBtn3Click(Sender: TObject);
var
  x: integer;
  b: boolean;
begin
  if true then
    x := 8    //********************** i try to extract this line
  else
    x := 6;

  showmessage(inttostr(x));
end;

the result i get is:

procedure TForm1.BitBtn3Click(Sender: TObject);
var
  x: integer;
  b: boolean;
begin
  if true then
    newMethode
  else
    x := 6;

  showmessage(inttostr(x));
end;

and the new Methode is:

procedure TForm1.newMethode;
var
  x: Integer;
begin
  x := 8;
end;

Can anybody check how is the behaviour on Delphi XE? anybody knows if it was reported?


Solution

  • This is a bug in the "Extract Method" refactoring.

    As an alternative, you might want to use the "Extract Method" refactoring from ModelMaker Code Explorer refactoring tool. At EUR 99, it is a relatively cheap tool that works from Delphi 5 onward, and the recent 9.0.5 updates have vastly improved their Extract Method refactoring so much that I haven't used the Delphi built-in one for quite a while.

    Two great benefits:

    • it launches the method-editor dialog where you can change and reorder parameters, which are then reflected in the extracted and calling code
    • it leaves the original code in a (* *) comment just in case something fails, or you need to reference it

    In addition, it places bookmarks (numbered 7, 8 and 9) in the code for easy navigation between the extracted code and the call site.

    Highly recommended.