Show TOC

Extracting Local Variables from ExpressionsLocate this document in the navigation structure

Context

In a method of an ABAP class, you can create a new variable on the basis of an expression. This means, the tool adds a new local variable and assigns the expressions.

Note Other occurrences of the same expression are not replaced with the new variable.

Example

The expression length * width is to be extracted into a new local variable:

CLASS cl_local_variable_expression IMPLEMENTATION. 
 METHOD check_area. 
  DATA: length, width, result TYPE i. 
  IF ( length * width > 100 ). 
   result = abap_true. 
  ENDIF. 
 ENDMETHOD. 
ENDCLASS.

Procedure

  1. In the implementation part, select the entire expression that you want to replace with a new variable.
    Extracting an expression into a local variable
    Figure 1: Extracting an expression into a local variable
  2. In the context menu, choose Quick Fix or use the shortcut (Ctrl 1).
  3. In the Quick Fix dialog box, double-click Extract local variable.

Results

In the implementation part of the method, the new variable (area) is defined. The expression is assigned to the new variable. In the IF statement, the expression is replaced with the new variable.

Example

ABAP class after extraction:

CLASS cl_local_variable_expression IMPLEMENTATION. 
 METHOD check_area. 
  DATA: length, width, result TYPE i,
      area TYPE p. 
  area = length * width. 
  IF ( area > 100 ). 
   result = abap_true. 
  ENDIF. 
 ENDMETHOD. 
ENDCLASS.