Refactoring - Extracting Method

From RAD Studio
Jump to: navigation, search

Go Up to Refactoring Procedures Index


Editing Delphi code in the Code Editor, you can use the Refactor > Extract Method menu command to create a method from a code fragment.

To extract a method:

  1. In the Code Editor, open the class or interface containing the code fragment that you wish to extract.
  2. Place the mouse cursor in the desired fragment of source code. Refactoring determines the beginning and the end of the relevant statement.
  3. On the main menu, choose Refactor > Extract Method.

Tip: Alternatively, right-click the code fragment and choose Refactor > Extract Method on the context menu.

  1. In the Extracting Method dialog box, enter a name for the method in the New method name field, or accept the suggested name.
  2. Review the code in the Sample extracted code pane.
  3. Click OK to complete the extraction and create the new method.

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.

Tips:

  • When applying Extract Method, parameters and local variables in the selected code fragment become the parameters of the new method.
  • The code fragment cannot contain a return statement of the original method. An error message displays if you attempt to include a return statement in the code fragment.
  • The code fragment cannot modify more than one single local variable. An error message displays if you violate this restriction.
  • If the selected code fragment is repeated in several locations, it is your responsibility to replace these fragments in the appropriate locations with the proper method calls.

See Also