Show TOC

Extracting Constants from LiteralsLocate this document in the navigation structure

In the source code editor of a development object, you can substitute literals with constants. This enables you to replace a specific, unchanging value that is used several times and 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 constant:

  • The number 18 with the constant "voting_age"
  • The message 'Hello World' with a generated constant "hello_world" 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 constant:

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

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

  • Extract a local constant from a literal that is used only in the current method. The new constant definition is created in the current method. Here all occurrences of the literal are replaced. Extract a member constant from a literal to define it in the private section of the current ABAP class. Here all occurrences of the literal are replaced.
  • Use an existing constant with the same value as the literal.
Results

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

The name of the generated constant is derived from the literal and automatically generated. These are the rules for the creation of the constant name:

  • Spaces are replaced by underscores.
  • Invalid characters (for example &, %, blanks, and so on) 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.)

Limitations

The following limitations exist for extracting constants from literals:

  • Member constants are always created as private members.
  • If the same literal is already used in other classes or interfaces, these instances are not replaced.
  • Expressions cannot be extracted to a constant, because in ABAP they are allowed in constant definitions.
  • This option is provided only within ABAP classes and interfaces. Global constants in reports and ABAP function groups are currently not supported.