Show TOC

Extracting Variables from LiteralsLocate this document in the navigation structure

In the source code editor of a development object, you can substitute literals with variables. This enables you to replace a specific, unchanging value that is used several times or to improve the readability of ABAP source code. However, literals should only be extracted if the abstraction results in a compliant solution.

Note Literals are separated into:
  • Textual literals: character, string, string template
  • Numeric literals: integer (> i), float (> decfloat 16)

Example

In the following examples, literals can be replaced with a variable:

  • The number 50 with the constant "max_speed"
  • The message 'Hello Planet' with a generated constant "hello_planet" wherever it is used in the source code of a certain development object

The following examples display the extraction of a certain literal in a variable:

Literal Type Example Constant Name Constant Type Constant Value
Character 'Hello Planet !' hello_planet String Float
'Hello Planet !' 'Hello Planet !' hello_planet String 'Hello Planet !'
String Template |Hello { name }!| hello String 'Hello ' or '!'
Integer 255 _255 i 255
Float '3.141529' _3141529 decfloat16 '3.141529'
Limitations

The following limitations apply when extracting variables from literals:

  • Member variables are always created as private members.
  • If the same literal is already used in other classes or interfaces, these instances are not replaced.
  • Currently, expressions cannot be extracted to a variable.
  • This option is provided only within ABAP classes and interfaces. Global variables in reports and ABAP function groups are currently not supported.
Functionalities

You can extract variables from a literal using the following refactoring functions:

  • Extract a local variable from a literal that is used only in the current method. The new variable definition is created in the current method. Here all occurrences of the literal are replaced.
  • Extract an attribute from a literal to define it in the private section of the current ABAP class. Here all occurrences of the literal are replaced. Attributes are always created as private members.
Results

When you extract a variable in the source code of a development object, the definition DATA 'your name' TYPE <type> VALUE <value> is added. Also, in the development object the corresponding literal is replaced with the variable.

The name of the generated variable is derived from the literal and automatically generated. The following limitations exist for the creation of the variable name:

  • Spaces are replaced by underscores.
  • Invalid characters (for example &, %, blanks, etc.) are eliminated.
  • The maximum length is 30 characters.
  • If the literal starts with a number, an underscore prefix is set.

Finally, a pragma (##NO_TEXT) is added in the declaration. Therefore, no translation warnings will be issued by the ABAP Compiler syntax check.