RAD Studio (Common)
ContentsIndex
PreviousUpNext
Refactoring Code

Refactoring refers to the capability to make structural changes to your code without changing the functionality of the code. Code can often be made more compact, more readable, and more efficient through selective refactoring operations. RAD Studio provides a set of refactoring operations that can help you re-architect your code in the most effective and efficient manner possible.  

Refactoring operations are available for Delphi, C#, and C++. However, the refactorings for C# and C++ are limited in number. You can access the refactoring commands from the Refactoring menu or from a right-click context menu while in the Code Editor

The Undo capability is available for all refactoring operations. Some operations can be undone using the standard Undo (CTRL+Z) menu command, while the rename refactorings provide a specific Undo feature.

To rename a symbol

  1. In the Code Editor, click the identifier to be renamed. The identifier can be a method, variable, field, class, record, struct, interface, type, or parameter name.
  2. From either the main menu or the Code Editor context menu, choose RefactorRename.
  3. In the Rename dialog box, enter the new identifier in the New Name field.
  4. Leave View references before refactoring checked. If this option is unchecked, the refactoring is applied immediately, without a preview of the changes.
  5. Click OK. The Refactorings dialog box displays every occurrence of the identifier to be changed.
  6. Review the proposed changes in the Refactorings dialog box and use the Refactor button at the top of the dialog box to perform all of the refactorings listed. Use the Remove Refactoring button to remove the selected refactoring from the dialog box.

To declare a variable

  1. In the Code Editor, click anywhere in a variable name that has not yet been declared.
    Note: Any undeclared variable will be highlighted with a red wavy underline by Error Insight.
  2. From either the main menu or the Code Editor context menu, choose RefactorDeclare Variable. If the variable has already been declared in the same scope, the command is not available.
  3. Fill in the Declare New Variable dialog box as needed.
  4. Click OK.
The variable declaration is added to the procedure, based on the values you entered in the Declare New Variable dialog box.

To declare a field

  1. In the Code Editor, click anywhere in a field name that has not yet been declared.
  2. From either the main menu or the Code Editor context menu, choose RefactorDeclare Field.
  3. Fill in the Declare New Field dialog box as needed.
  4. Click OK.
The new field declaration is added to the type section of your code, based on the values you entered in the Declare New Field dialog box.
Note: If the new field conflicts with an existing field in the same scope, the Refactorings
dialog box is displayed, prompting you to correct the conflict before continuing.

To create a method from a code fragment

  1. In the Code Editor, select the code fragment to be extracted to a method.
  2. From either the main menu or the Code Editor context menu, choose RefactorExtract Method. The Extract Method dialog box is displayed.
  3. Enter a name for the method in the New method name field, or accept the suggested name.
  4. Review the code in the Sample extracted code window.
  5. Click OK.
RAD Studio moves the extracted code outside of the current method, determines the needed parameters, generates local variables if necessary, determines the return type, and replaces the original code fragment with a call to the new method.

To convert a string constant to a resource string (for the Delphi language only)

  1. In the Code Editor, select the quoted string to be converted to a resource string, for example, in the following code, insert the cursor into the constant Hello World:

procedure foo;
begin
    writeLn('Hello World');
end;

  1. From either the main menu or the Code Editor context menu, choose RefactorExtract Resource String.
    Note: You can also use the Shift+Ctrl+L
    keyboard shortcut. The Extract Resource String dialog box is displayed.
  2. Enter a name for the resource string or accept the suggested name (the Str, followed by the string).
  3. Click OK.
The resourcestring keyword and the resource string are added to the implementation section of your code, and the original string is replaced with the new resource string name.

resourcestring
    strHelloWorld = 'Hello World';

procedure foo;
begin
    writeLn(StrHelloWorld);
end.
To find and add a namespace or unit to the uses clause

  1. In the Code Editor, click anywhere in a the variable name whose unit you want to add to the uses clause (Delphi) or the namespace you want to add to the using clause (C#).
  2. From either the main menu or the Code Editor context menu, choose RefactorFind Unit. The Find Unit dialog box displays a selection list of applicable Delphi units.
    Note: If you are coding in C#, the dialog box is called the Use Namespace
    dialog box.
  3. Select the unit or namespace that you want to add to the uses or using clause in the current scope. You can select as many units or namespaces as you want.
  4. If you are coding in Delphi, choose where to insert the reference, either in the interface section or in the implementation section.
    Note: This choice is not relevant for C# and so the selection is not available when refactoring C# code.
  5. Click OK.
The uses or using clause is updated with the selected units or namespaces.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!