SAP LITERALS - Guide
Get Example source ABAP code based on a different SAP table
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:
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 numeric 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 multiple 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.
Numeric literals that appear in source code seemingly without any semantic meaning are often known as
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.
Latest notes:
If literals are used, ensure that literals are actually used, not equivalent expressions:
The expressions are not evaluated until runtime, meaning that they have a lower performance. This distinction is irrelevant only if the literals are parts of expressions anyway.
ABAP_HINT_END
ABAP_EXAMPLE_BAD
The following source code uses the same literal multiple times to specify
circumference TYPE decfloat34,
area TYPE decfloat34.
...>
2* '3.141592653589793238462643383279503' * radius.
area =
'3.141592653589793238462643383279503' * radius ** 2.>
ABAP_EXAMPLE_END
ABAP_EXAMPLE_GOOD
The following source code declares a constant that requires the literal the value of
VALUE '3.141592653589793238462643383279503'.>
circumference TYPE decfloat34,
area TYPE decfloat34.
...
circumference = 2 * pi * radius.
area = pi * radius ** 2.>
ABAP_EXAMPLE_END