Skip to content

ABAP Keyword Documentation →  ABAP Programming Guidelines →  Robust ABAP →  Data Types and Data Objects 

Data Objects for Truth Values

Other versions: 7.31 | 7.40 | 7.54

Background

Truth values are results of logical expressions. A truth value is either true or false. ABAP does not yet support Boolean data types and thus does not support data objects for truth values. Therefore, the result of a logical expression cannot be assigned directly to a data object.

It has become common practice to express the truth value "true" as value "X" and the truth value "false" as a blank (" "). There are also Boolean functions that have a logical expression as an argument and are returned as the value "X" or a blank, depending on the result.

To make it easier to handle truth values expressed in this way, the type group abap contains a data type abap_bool of elementary type c with length 1, and the constants abap_true of value "X" and abap_false of value " " as substitutes for a real Boolean data type. There is also a constant abap_undefined of value "-".

Rule

Use the data type abap_bool for truth values

When working explicitly with truth values, use the type abap_bool as a substitute for a real Boolean data type. A data object declared in this way should have no values other than the relevant constants abap_true and abap_false (also abap_undefined).

Details

Using the type abap_bool and the constants abap_true and abap_false makes it clear that truth values are being used here.

In accordance with the rule for avoiding literals in operand positions, not only the literals 'X' and ' ' should be used. State queries about the predicate operators IS INITIAL and IS NOT INITIAL or the use of the constant space are also not advisable, because they require knowledge of the technical values of abap_true and abap_false, which are not significant in the sense of real Boolean data objects.

The type group abap contains a third constant for the type abap_bool, namely abap_undefined. However, implementing a three-value logic is only useful and recommended in exceptional cases. In this case, note that abap_undefined does not contain the initial value for a variable of type abap_bool. The initial value is always the value of abap_false. However, the value of abap_undefined can, if required, be specified using the addition VALUE when declaring a truth value as the start value.

Bad Example

The following source code shows an unsuitable emulation of the Boolean data objects not present in ABAP.

DATA is_found TYPE c LENGTH 1.
...
is_found = 'X'.
...
IF is_found IS NOT INITIAL.
   ...
ENDIF.

Good Example

The following source code shows the recommended emulation of the Boolean data objects not present in ABAP.

DATA is_found TYPE abap_bool.
...
is_found = abap_true.
...
IF is_found = abap_true.
   ...
ENDIF.