Show TOC

Extracting Methods from StatementsLocate this document in the navigation structure

Prerequisites

You must be editing a global or local ABAP class in a class pool, a program, or another type of ABAP program. Method extraction is not supported in code outside of classes. Also, you can only extract a method within the start and end area of another method.

Your code should be free of syntax errors.

Context

In the implementation of an ABAP class, you can create a new method. The selected code is moved into the body of the new method and replaced with a call to the new method. Further occurrences of similar code are not replaced.

Note The name of the new method is derived from the first code line that is selected. To this end, you can add a comment at the beginning of the source code that contains the keywords you want to reuse in the name of the new method.
Example of selected source code that is extracted into a new method
Figure 1: Example of selected source code that is extracted into a new method

Procedure

  1. In the source code editor, select the code that you want to move to a new method.
  2. In the context menu, choose Start of the navigation path Source Code Next navigation step Extract Method ... End of the navigation path or the shortcut (Alt Shift M) to start the dialog box.

Input field to edit the name of the method to be extracted
Figure 2: Input field to edit the name of the method to be extracted

You can change a suggested method name as well as the suggested name and attributes of a method parameter.

Note You cannot add a parameter to the proposed method signature, nor can you delete one.

  1. Chose Next to open the transport dialog:
  2. [Optional:] If the changed object is not assigned to a transport request, choose Change Transport to select an existing or create a new transport request.
    • Select Error handling if you want to ignore syntax errors. Consequently, no error messages will be displayed.
    • Select Activation if you want to activate the affected objects after refactoring. As a result, the changes will also be available in the related objects.
      Note The method extractor presents the ABAP Compare editor to display the changes that will be made.
  3. Chose Next to check the changes that will be made. The method extractor presents the ABAP Compare editor to display the changes that will be made.
  4. Use the ABAP Compare navigation buttons to review the changes. These are all shown in the Refactored Source editor to the right.

Navigation between original and refactored changes
Figure 3: Navigation between original and refactored changes
Note
  • Comment as default name: Alternatively, you can start your selection with a comment. The comment is then automatically suggested as the name of the new method. But you can also change the name or write in your own name from scratch.
  • Method signature: The code extractor is very intelligent and usually does a good job of parsing the code to extract and suggest useful method parameters. However, it can – under rare circumstances – be misled. In this case, you can adjust the extracted code after you are finished with the method extraction. You can also, within reason, change the suggested direction (exporting/changing...) of a parameter.
  • Method visibility: By default, the new method is a private method in the local or global class you are currently working in. You cannot have the method created in another class.

  1. Choose Finish to complete the method extraction.

Results

The method extractor inserts a call to the new method. Using Undo Extract Method (Ctrl Z) in the editor context menu, you can cancel the method extraction and revert to the old code.

In addition to moving the extracted code and replacing it with a method call, the method extractor has done the following:

  • Implemented the signature – with your changes – that it suggested for the new method
  • Declared any local variables that are needed in the new method
  • Exposed class-based or classic exceptions in the method signature
  • Suggested a simple, recommended naming convention for parameters and variables in the new method

You may wish to delete unused variables in the class from which you extracted the method. The method extractor does not change the data declarations in your code, so the method extraction may leave an orphaned data declaration behind.