Boolean in ABAP

Dealing with two state flags is nothing new and a very common scenario in ABAP development, Historically they have been used to represent a true or false state.
For example: Y or N, X or ' ', 0 or 1.

Booleans are data objects that store truth values i.e. True or False, which are results of logical expressions.

Truth values are the 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. However, it can be achieved using Boolean functions.

It has become common practice to express the truth value "true" as value "X" and the truth value "false" as a blank (" "). 

which leads to one of the bad practices still prevalent:

data:
lv_myflag TYPE C length 1.
lv_myflag = 'X'.
IF lv_myflag IS NOT INITIAL.
"Some logic..
ENDIF.

Using the Data Type abap_bool for Truth Values


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 "-".

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).

Using the type abap_bool and the constants abap_true and abap_false makes it clear that you are working with truth values.

Transforming the above-mentioned example using boolean subsitues:

data:
lv_myflag TYPE abap_bool.
lv_myflag = abap_true.
IF lv_myflag = abap_true.
"Some logic..
ENDIF.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP