Show TOC

Extracting Constants from LiteralsLocate this document in the navigation structure

You can extract local and member constants, local variables, attributes, as well as importing parameters from the source code in order to substitute a literal in all or selected methods of an ABAP class. Afterwards, the tool adds the corresponding identifier in the private or public section of the current class. So, the new identifier is available in the entire ABAP class or in the corresponding method.

Context

Example

Before Execution After Execution
CLASS cl_car_extract_constant DEFINITION PUBLIC CREATE PUBLIC . 
 PUBLIC SECTION . 
  DATA current_speed TYPE i. 
  METHODS: drive IMPORTING i_speed TYPE i. 
 PRIVATE SECTION . 
ENDCLASS. 
 
CLASS cl_car_extract_constant 
 METHOD drive. 
  IF i_speed > 240. 
   current_speed = 240. 
  ENDIF. 
 ENDMETHOD. 
ENDCLASS. 

To copy the source code example, click hereCode Example Before Execution

CLASS cl_car_extract_constant DEFINITION PUBLIC CREATE PUBLIC .
 PUBLIC SECTION .
  DATA current_speed TYPE i.
  METHODS: drive IMPORTING i_speed TYPE i.
 PRIVATE SECTION .
  CONSTANTS _240 TYPE i VALUE 240.
ENDCLASS.
 
CLASS cl_car_extract_constant
 METHOD drive.
  IF i_speed > _240.
   current_speed = _240.
  ENDIF.
 ENDMETHOD.
ENDCLASS.
The literal '240' is to be extracted into a member constant. In the private section of the ABAP class, the new constant (_240) is defined as a string type with the value of the literal ('240'). Also, all occurrences of the literal are replaced with the new constant.

Procedure

  1. In the implementation part, select the literal that is to be extracted as a member constant.
  2. Get the quick assist proposals by choosing Quick Fix from the context menu (shortcut Ctrl 1) or by using the Quick Assist view.
  3. In the Quick Fix dialog box, double-click Extract local constant or Extract member constant.