ABAP BOOLC and XSDBOOL use-cases
In the previous post ABAP Convert Logical Expressions Into Boolean Values, I talked about BOOLC and XSDBOOL functions and their respective features. I thought I would elaborate on their use cases with examples.
BOOLC:
- Set boolean flag:
- Set custom flags like Y/N or 0/1 using ABAP built-in processing functions.
- Pass return to methods expecting ABAP_BOOL parameters,
When not to use BOOLC:
Comparison against ABAP_BOOL types:Despite the name BOOL and C, the function doesn't return ABAP_BOOL type nor a C.
This leads to unexpected behavior, for example, this code prints "instead I will be printed."
Even though 1 = 2 is false , boolc returns a string with space which is compared to a converted string with ignored spaces( BLANK) hence the comparison fails.
XSDBOOL ( available from ABAP 740 SP08 )
- Set boolean flag:
- Pass return to methods expecting ABAP_BOOL parameters.
- Comparison against ABAP_BOOL.
When not to use XSDBOOL:
Comparison with STRING types:( Hope you see the pattern here)
As discussed above,Since XSDBOOL returns ABAP_BOOL, the false results get ignored.
Picking the same example from BOOLC to set a custom flag,
This code doesn't work as expected due to conversion rules being at play.
I hope you liked the BOOLC and XSDBOOL use cases.
Both of the Boolean functions have their use cases and should be used appropriately. The pitfalls around their usage come due to the nature of ABAP comparison rules and should be kept in mind.