SAP LITERALS - Guide
Get Example source ABAP code based on a different SAP table
GUIDELINE 6.8
Literals
ABAP_BACKGROUND
A literal is a data object defined in the source code of a program by specifying a character-like value. Possible literals are:
Sequence of digits with an optional plus/minus sign. The data type is either
In numeric literals, neither decimal separators nor scientific notation with mantissa and exponent are possible. Character literals with
ABAP_RULE
Avoid using literals to specify values directly in the source code. Instead constants should be declared with these values. This applies to numerical values in particular.
ABAP_DETAILS
Certain values are required in more than one place in the source code. It is therefore not helpful to specify these values directly in the source code, since several statements would need to be modified each time the value is changed. An appropriate constant can be used instead to enable the value to modified at a central position in the source code. Literals are, of course, allowed when specifying values in the declaration of the constants. This can make the program significantly easier to maintain and enhance at a later date.
It can also be a good idea to create an appropriate constant for values used in only one place. The name of the constant gives the values semantics that make the source code easier to understand.
Number literals that appear in source code seemingly without any semantic meaning are often known as 'magic numbers'. Avoid using these in ABAP programs. When using character literals,
Exception
In certain situations, however, using constants can affect the readability of a program. It is better to specify a literal in these cases, as in the following examples:
These examples make the semantic meaning of the literals clear and translatability is not an issue.
Another area where character literals are vital is dynamic programming. Here, parts of statements or entire programs are generated, which is virtually impossible without using character literals. In addition, string templates offer various enhanced options for using literal texts.
Bad example
The following source code uses the same literal multiple times to specify pi (#) in operand positions. This multiple use makes the program unclear and error-prone.
circumference TYPE decfloat34,
area TYPE decfloat34.
...
2* '3.141592653589793238462643383279503' * radius.
area =
'3.141592653589793238462643383279503' * radius ** 2.
Good example
The following source code declares a constant that requires the literal with the value pi (#) only once, and uses it in the appropriate operand positions.
VALUE '3.141592653589793238462643383279503'.
circumference TYPE decfloat34,
area TYPE decfloat34.
...
circumference = 2 * pi * radius.
area = pi * radius ** 2.