14.01.2020 Views

ABAP_to_the_Future

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Conditional Logic 2.5

If the functional method in Listing 2.35 returns a string and that string is NO NO!

Do not blow up the world, whatever you do, do not blow up the world, then

the result is NOT INITIAL and thus evaluated as true, and so it is curtains for all of

us.

2.5.3 Using XSDBOOL as a Workaround for BOOLC

Another common situation with respect to Boolean logic (or the lack thereof)

within ABAP is a case in which you want to send a TRUE/FALSE value to a method

or get such a value back from a functional method. In ABAP, you cannot just say

something like the following:

RF_IS_A_MONSTER = ( LD_STRENGTH > 100 AND LD_SANITY < 20)

Although, in some programming language s you can do precisely that (can you

guess which computer could do that in 1981?). Again, we have a workaround in

the form of the built-in function BOOLC (Listing 2.36).

* Postconditions

zcl_dbc=>ensure( id_that = 'A result table is returned'(005)

if_true = boolc( et_return[] IS NOT INITIAL ) ).

Listing 2.36 BOOLC

In Listing 2.36, you pass in a TRUE/FALSE value based on whether an internal table

has any entries. This works fine.

However, what if you want to test for a negative using this method? Say, for

example, that you want to pass in to parameter IF_TRUE a TRUE/FALSE value that’s

true if the table is empty. If yo u use the previous technique using BOOLC, then

things start going horribly wrong. This can be demonstrated by running the code

in Listing 2.37.

DATA: lt_empty TYPE STANDARD TABLE OF ztvc_monsters.

IF boolc( lt_empty[] IS NOT INITIAL ) = abap_false.

WRITE:/ 'This table is empty'.

ELSE.

WRITE:/ 'This table is as full as full can be'.

ENDIF.

IF boolc( 1 = 2 ) = abap_false.

WRITE:/ '1 does not equal 2'.

ELSE.

105

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!