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
- 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.
- From either the main menu or the Code Editor context menu, choose RefactorRename.
- In the Rename dialog box, enter the new identifier in the New Name field.
- Leave View references before refactoring checked. If this option is unchecked, the refactoring is applied immediately, without a preview of the changes.
- Click OK. The Refactorings dialog box displays every occurrence of the identifier to be changed.
- 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
- 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.
- 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.
- Fill in the Declare New Variable dialog box as needed.
- 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
- In the Code Editor, click anywhere in a field name that has not yet been declared.
- From either the main menu or the Code Editor context menu, choose RefactorDeclare Field.
- Fill in the Declare New Field dialog box as needed.
- 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
- In the Code Editor, select the code fragment to be extracted to a method.
- From either the main menu or the Code Editor context menu, choose RefactorExtract Method. The Extract Method dialog box is displayed.
- Enter a name for the method in the New method name field, or accept the suggested name.
- Review the code in the Sample extracted code window.
- 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)
- 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;
- 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.
- Enter a name for the resource string or accept the suggested name (the Str, followed by the string).
- 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
- 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#).
- 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.
- 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.
- 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.
- Click OK.
The
uses or
using clause is updated with the selected units or namespaces.