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.
Literals are separated into:
Example
In the following examples, literals can be replaced with a constant:
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' |
You can extract constants from a literal using the following refactoring functions:
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:
Finally a pragma (##NO_TEXT) is added in the declaration. Therefore, no translation warnings will be issued by the ABAP Compiler syntax check.)
The following limitations exist for extracting constants from literals: