Show TOC

Extracting Constants from LiteralsLocate this document in the navigation structure

Context

You can extract:

  • Local and member constants
  • Local variables
  • Attributes
  • 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.

Example

The literal '240' is to be extracted into a member constant:

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. 

Procedure

  1. In the implementation part, select the literal that is to be extracted as a member constant.
  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 constant or Extract member constant.

Results

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.

Example

ABAP class after extraction:

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.